Hi,
I have modified your example a bit, Now this class is overriding the method in each class.
Because in over-riding the method, the method signature remains the same. Now it will
give the expected output, you are mentioning above.
class Mammal{
void eat(Mammal m){
System.out.println("Mammal eats food");
}
}
class Cattle extends Mammal{
void eat(Mammal c){
System.out.println("Cattle eats hay");
}
}
class Horse extends Cattle{
void eat(Mammal 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);
}
}
Hope this helps,
Ben