When a child class overrides multiple methods and calls a method in its parent class, does the parent class use it’s own pre-defined methods, or the ones that the child has overridden?
For some context, and an example, my question stems from this question in the AP Computer Science Curriculum. Below, when super.act() is called, the act method from Dog calls eat(). Does that eat call go to 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");
}
}
Assume that the following declaration appears in a client program.
Dog fido = new UnderDog();
What is printed as a result of the call fido.act()?
run eatrun eat sleeprun eat sleep barkrun eat bark sleep- Nothing is printed due to infinite recursion.
There are two ways a subclass can call a superclass method:
The syntax for the first way is as follows:
the syntax for the second way of invocation is as follows:
I think now you have enough information to trace what happens in your example without further help.