Single Choice Not Set

QWhat is the result of compiling and running this program?
class Mammal{
      void eat(Mammal m){
            System.out.println("Mammal eats food");
      }
}
class Cattle extends Mammal{
      void eat(Cattle c){
            System.out.println("Cattle eats hay");
      }
}
class Horse extends Cattle{
      void eat(Horse h){
            System.out.println("Horse eats hay");
      }
}
public class Test{
      public static void main(String[] args){
            Mammal h = new Horse();
            Cattle c = new Horse();
            c.eat(h);
      }
}

ID: #2257 Inheritance in Java MCQ 3,407 views
Question Info
#2257Q ID
Not SetDifficulty
Inheritance in Java MCQTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A prints "Mammal eats food"
  • B prints "Cattle eats hay"
  • C prints "Horse eats hay"
  • D Class cast Exception at runtime.
Correct Answer

Explanation

prints "Mammal eats food"

Share This Question

Challenge a friend or share with your study group.