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.
In the example below, the Student class overrides the getFood method of the Person class, and it uses super.getFood() to call the PersongetFood method before adding on to it. Here, a Person is associated with the food โHamburgerโ and a Student is associated with โHamburgerโ and โTacoโ.
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 commonly overridden. A subclass can override toString but in its new toString method, it can call super.toString() to get a string to which it can add its own instance variables.
// overridden toString() in subclass
public String toString()
{
return super.toString() + "\n" + subclassInstanceVariables;
}
Activity5.4.1.
Add another subclass called Vegan that inherits from the Student class. Add a Vegan contructor that takes a name as an argument and passes it to the super constructor. Override the getFood() method in Vegan to call the superclass getFood() but add a โNo โ in front of it and then say โbut โ and add a vegan food. Change Javier to a Vegan object in main() and try it out!
Given the following class declarations, and assuming that the following declaration appears in a client program: Base b = new Derived();, what is the result of the call b.methodOne();?
public class Base
{
public void methodOne()
{
System.out.print("A");
methodTwo();
}
public void methodTwo()
{
System.out.print("B");
}
}
public class Derived extends Base
{
public void methodOne()
{
super.methodOne();
System.out.print("C");
}
public void methodTwo()
{
super.methodTwo();
System.out.print("D");
}
}
This would be true if the object was created of type Base using new Base. But the object is really a Derived object. So all methods are looked for starting with the Derived class.
Even though b is declared as type Base it is created as an object of the Derived class, so all methods to it will be resolved starting with the Derived class. So the methodOne() in Derived will be called. This method first calls super.methodOne so this will invoke the method in the superclass (which is Base). So next the methodOne in Base will execute. This prints the letter "A" and invokes this.methodTwo(). Since b is really a Derived object, we check there first to see if it has a methodTwo. It does, so execution continues in Derivedโs methodTwo. This method invokes super.methodTwo. So this will invoke the method in the super class (Base) named methodTwo. This method prints the letter "B" and then returns. Next the execution returns from the call to the super.methodTwo and prints the letter "D". We return to the Base class methodOne and return from that to the Derived class methodOne and print the letter "C".
After the call to methodOne in the super class printing "A", the code continues with the implicit this.methodTwo which resolves from the current objectโs class which is Derived. methodTwo in the Derived class is executed which then calls super.methodTwo which invokes printin "B" from methodTwo in the Base class. Then the "D" in the Derive methodTwo is printed. Finally the program returns to methodOne in the Derived class are prints "C".
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.
Override the toString method in the OnlineCustomer class to call the super classโs toString method and then add on the email address. See the example above for help.