Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The superclass and subclass Undead and Vampire are designed as shown in the foll

ID: 3827858 • Letter: T

Question

The superclass and subclass Undead and Vampire are designed as shown in the following class diagram. The superclass Undead contains A protected String variable food. A private String variable comesOut. Two constructors -one takes no parameter. -one takes a String parameter food and assigns this value to the variable food. A public method setFood() that takes a String parameter and sets the variable food to the parameter's value. A public method getFood() that returns the value of food a public method eats() that prints 'The undead eats [food]". a public method comesOut() that prints 'The undead can come out (comesOut]". The subclass Vampire contains A private String variable name. Three constructors. -one takes no parameter - one takes a String parameter name and assigns this value to the variable name. - one takes two String parameters name and food and sets the variables name and food to these values. A public setName() method that takes a String parameter and sets name to the value of this parameter. A public getName() method that returns the value of name. A public method eats() that prints "[name] drinks [food]" A public method comesOut() that prints "[name] comes out [comesOut]". Implement the classes Undead and Vampire in the answer box below. Remember to leave out the word public in the class header.

Explanation / Answer

Here is a java implementation of classes :

note that if comesOut is private variable then it can not be accessed in sub classes. so it has been made protected to be able to access in subclass Vampire.

Code :

class Undead{
   protected String food;
   protected String comesOut;
   Undead(){
      
   }
   Undead(String f){
       food = f;
   }
   public void setFood(String f){
       food = f;
   }
   public String getFood(){
       return food;
   }
   public void eats(){
       System.out.println("The undead eats "+ food);
   }
   public void comesOut(){
       System.out.println("The undead can come out" + comesOut);
   }
  
}

class Vampire extends Undead {
   private String name ;
   Vampire(){
   }
   Vampire(String n){
       name = n;
   }
   Vampire(String n, String f){
       name = n;
       food = f;
   }
   public void setName(String n){
       name = n;
   }
   public String getName(){
       return name;
   }
   public void eats(){
       System.out.println(name + "drinks" + food);
   }
   public void comesOut(){
       System.out.println(name + "comes out"+ comesOut);
   }
}

if you have any doubts, you can ask in comment section.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote