?
What is the result of compiling and running this program?
What 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);
}
}
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);
}
}
Join the Discussion
Login to post a comment or share your explanation.
Login1-> eat(Mammal m)
2-> eat(Cattle c)
Similarly when Horse extends Cattle, then the method are not over ridden because of the different signature i.e. different parameters.
So when c.eat(h) is called then c is referencing to Horse object so we look in Horse class.. where we find three method..
1-> eat(Mammal )
2-> eat(Cattle )
3-> eat(Horse )
Now h is refererring to mammal, so eat(mammal) will be executed