When a child class overrides several methods and calls a method in its parent class, does the parent class use its own predefined methods or those that the child overrides?
In some context and example, my question stems from this question in the AP Computer Science Curriculum curriculum. Below, when super.act()
is called, the act
method of Dog
calls eat()
. Does the call to eat
call the eat
method in Dog
or UnderDdog
?
Consider the following two classes.
public class Dog { public void act() { System.out.print("run"); eat(); } public void eat() { System.out.print("eat"); } } public class UnderDog extends Dog { public void act() { super.act(); System.out.print("sleep"); } public void eat() { super.eat(); System.out.print("bark"); } }
Suppose the following ad appears in the client program.
Dog fido = new UnderDog();
What is printed as a result of calling fido.act()
?
run eat
run eat sleep
run eat sleep bark
run eat bark sleep
- Nothing prints due to infinite recursion.
source share