Using Super to call an Overridden MethodΒΆ

Sometimes you want the child class to do more than what a parent method is doing. You want to still execute the parent method, but then do also do something else. But, since you have overridden the parent method how can you still call it? You can use super.method() to force the parent’s method to be called.

How does this work? Remember that an object always keeps a reference to the class that created it and always looks for a method during execution starting in the class that created it. If it finds the method in the class that created it, it will execute that method. If it doesn’t find it in the class that created it, it will look at the parent of that class. It will keep looking up the ancestor chain until it finds the method. The method has to be there, or else the code would not have compiled.

When the student getFood() method is executed it will start executing the getFood method in Student. When it gets to super.getFood() it will execute the getFood method in Person. This method will return the string "Hamburger". Then execution will continue in the getFood method of Student and it return return the string "Hamburger and Taco".

You can step through this example using the Java Visualizer by clicking on the following link: Super Example.

Next Section - Access to Inherited Private Fields