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

Please do question number 2, and do not use handwriting. Just type it after you

ID: 3787820 • Letter: P

Question

Please do question number 2, and do not use handwriting. Just type it after you test the code.

1. Draw a class inheritance diagram for the following set of classes:

Class Goat extends Object and adds an instance variable tail and methods milk() and jump().

Class Pig extends Object and adds an instance variable nose and methods eat(food) and wallow().

Class Horse extends Object and adds instance variables height and color, and methods run() and jump().

Class Racer extends Horse and adds a method race().

Class Equestrian extends Horse and adds instance variable weight and is-Trained, and methods trot() and isTrained().

2. Consider the inheritance of classes from Exercise 1, and let d be an object variable of type Horse. If d refers to an actual object of type Equestrian, can it be cast to the class Racer? Why or why not?

Please do question number 2, and do not use handwriting. Just type it after you test the code.

Explanation / Answer

No, We can not caste a Equestrian to Racer.

class Goat extends Object{

   private String tail;

  

   public void milk(){

       System.out.println("GOAT: milk method....");

   }

  

   public void jump(){

       System.out.println("GOAT: jump method....");

   }

}

class Pig extends Object{

   private String nose;

  

   public void eat(String food){

       System.out.println("PID: eat method....");

   }

  

   public void wallow(){

       System.out.println("PIG: wallow method....");

   }

}

class Horse extends Object{

   private String color;

   private double height;

  

   public void run(){

       System.out.println("HORSE: run method....");

   }

  

   public void jump(){

       System.out.println("HORSE: jump method....");

   }

}

class Racer extends Horse{

  

   public void race(){

       System.out.println("Racer: race method....");

   }

}

class Equestrian extends Horse{

  

   private boolean is_Trained;

   private double weight;

  

   public void trot(){

       System.out.println("Equestrian: trot method....");

   }

  

   public boolean isTrained(){

       System.out.println("Equestrian: isTrained method....");

       return is_Trained;

   }

}

public class AnimalTest{

  

   public static void main(String[] args) {

      

       Horse horse = new Equestrian();

       Racer r = (Racer)horse; // RUN Time Error: We can not cast a Equestrian to Racer

   }

}