Java Program to Use Method Overriding in Inheritance for Subclasses
Method overriding in Java is when a subclass implements a method that is already present inside the superclass. With the help of method overriding we can achieve runtime polymorphism. When we are overriding a method then we must keep three things in mind.
- The method in the subclass must have the same name as the method present inside the superclass.
- The method in the subclass should have the same number of parameters.
- The return type (or sub-type) of the method in the subclass should be the same as the return type of the method inside the superclass.
In this article, we are going to discuss how we can use method overriding in the Inheritance of subclasses.
Let us understand the overriding of the methods of subclasses with the figure given below.
In the above figure, we have taken the example of the Grandpa class containing only one method i.e. show(). Dad class inherits the Grandpa class and overrides the show() method. After that Dad class is inherited by Me class which overrides the show() method of Dad class. After that, if we want to access the show() method of the above classes then we can make the object of that class since the method which would be invoked solely depends upon the type of the object but not on the type of reference variable referring to that object.
Let us understand the above concept using code.
Inside show() method of Grandpa class Inside show() method of Dad class Inside show() method of Me class
What happens if a subclass contains methods other than that of the parent/ superclass?
Let us understand this with the help of the figure mentioned below.
In the figure above we have taken one more method named display() inside the Dad class which is being overridden inside the Me class. Now if we try to access the display() method by making objects of Dad and Me class respectively and reference variable of Grandpa class, then it will give an error because a reference variable of that class can only be used to access the methods of subclasses that have been overridden from it down the hierarchy, ie we can’t use here reference variable of Grandpa class while calling display function, since Grandpa class is topmost in the hierarchy and it does not contain display function.
We will get the below error if we try to invoke the display() function using the reference variable of Grandpa() class.
dad.display(); me.display(); GFG.java:52: error: cannot find symbol dad.display(); ^ symbol: method display() location: variable dad of type Grandpa GFG.java:53: error: cannot find symbol me.display(); ^ symbol: method display() location: variable me of type Grandpa
We can solve this problem by making our reference variable of Dad type or Me type refer to the object of Dad and Me class using the syntax mentioned below.
Dad dad1 = new Dad(); Dad me1 = new Me(); dad1.display(); me1.display();
Now, let us understand these concepts with the help of the code mentioned below.
Inside show() method of Grandpa class Inside show() method of Dad class Inside show() method of Me class Inside display() method of class B Inside display() method of class C