How do parent classes function when one of their methods are overridden in a child?

inheritance, java

Solution

There are two ways a subclass can call a superclass method:

- Call a method defined in the superclass as its own, or

- Call a method defined in the superclass specifically requesting superclass's behavior

The syntax for the first way is as follows:

act(); // it's the same as this.act()

the syntax for the second way of invocation is as follows:

super.act();

I think now you have enough information to trace what happens in your example without further help.

Problem

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 eat` - `run eat sleep` - `run eat sleep bark` - `run eat bark sleep` - Nothing is printed due to infinite recursion.

Original source