Sometimes you want the subclass to do more than what a superclass’ method is doing. You want to still execute the superclass method, but you also want to override the method to 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.
We’ve used super() before to call the superclass’ constructor. There are two uses of the keyword super:
super(); or super(arguments); calls just the super constructor if put in as the first line of a subclass constructor.
super.method(); calls a superclass’ method (not constructors).
The keyword super is very useful in allowing us to first execute the superclass method and then add on to it in the subclass.
In this example, the Student class overrides the getFood() method of the Person() class, and it uses super.getFood() to call the Person getFood() method before adding on to it. Here, a Person is associated with the food “Hamburger” and a Student is associated with “Hamburger” and “Taco”.
Add another subclass called Vegan that inherits from the Student class. Override the getFood() method to call the superclass getFood() but add a “No “ in front of it and then add a vegan food. Change Javier to a Vegan and try it out!
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, all the way up to the Object class. 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 return the string
"Hamburger and Taco".
The toString() method is a common method that is overriden. A subclass can override the superclass toString() method and call the super.toString() before adding on its own instance variables.
// overriden toString() in subclass
public String toString()
return super.toString() + "\n" + subclassInstanceVariables;
Programming Challenge : Customer Info¶
The Customer class below keeps track of the names and addresses of customers. It has a toString() method that prints out the name and address of the object.
Create a subclass OnlineCustomer that inherits from the Customer class and adds a new instance variable for the email address of a online customer.
Override the toString() method in the OnlineCustomer class to call the super class toString() method and then add on the email address. See the example above for help.
Test the class by uncommenting the OnlineCustomer objects in the main method.
The keyword super can be used to call a superclass’s constructors and methods.
The superclass method can be called in a subclass by using the keyword super with the method name and passing appropriate parameters.