Java Program to Use Method Overriding in Inheritance for Subclasses
Last Updated :
28 Jan, 2021
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.
Java
// Java Program to show the overriding using inheritance in
// subclasses
// Grandpa class is at the top of
// the inheritance hierarchy
class Grandpa {
public void show()
{
System.out.println(
"Inside show() method of Grandpa class");
}
}
class Dad extends Grandpa {
// Overriding show method of Grandpa class
@Override public void show()
{
System.out.println(
"Inside show() method of Dad class");
}
}
// class Me is inheriting Dad class (i.e.
// a subclass of Grandpa class)
class Me extends Dad {
// Overriding show method of Dad class
@Override public void show()
{
System.out.println(
"Inside show() method of Me class");
}
}
public class GFG {
public static void main(String[] args)
{
// Creating instance of Grandpa class
Grandpa grandpa = new Grandpa();
// Creating instance of Dad class
Grandpa dad = new Dad();
// Creating instance of Me class
Grandpa me = new Me();
// as discussed which show() function will get
// execute depends upon the type of object
// show function of Grandpa class will get execute
grandpa.show();
// show function of Dad class will get execute
dad.show();
// show function of Me class will get execute
me.show();
}
}
OutputInside 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.
Java
// Java Program to show the overriding using inheritance in
// subclasses when subclass contains methods other than that
// of the parent/ superclass
// Grandpa class is at the top of
// the inheritance hierarchy
class Grandpa {
public void show()
{
System.out.println(
"Inside show() method of Grandpa class");
}
}
class Dad extends Grandpa {
// Overriding show method of Grandpa class
@Override public void show()
{
System.out.println(
"Inside show() method of Dad class");
}
// Creating a display method() for Dad class
public void display()
{
System.out.println(
"Inside display() method of class B");
}
}
// class Me is inheriting Dad class (i.e.
// a subclass of Grandpa class)
class Me extends Dad {
// Overriding show method of Dad class
@Override public void show()
{
System.out.println(
"Inside show() method of Me class");
}
@Override public void display()
{
System.out.println(
"Inside display() method of class C");
}
}
public class GFG {
public static void main(String[] args)
{
// The code below works the same as in
// previous example
Grandpa grandpa = new Grandpa();
Grandpa dad = new Dad();
Grandpa me = new Me();
grandpa.show();
dad.show();
me.show();
// Making Dad type point to the
// Dad object
Dad dad1 = new Dad();
// Making Dad type reference variable point to the
// Me object
Dad me1 = new Me();
dad1.display();
me1.display();
}
}
OutputInside 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
Similar Reads
Java Program to Use Method Overloading for Printing Different Types of Array
In Java method overloading can be defined as the class containing multiple methods with the same name but the list of parameters or type of parameters or the order of the parameters of the methods should not be the same. We can print different types of arrays using method overloading in java by maki
3 min read
How to Override compareTo() Method in Java?
As we know, there are basically two types of sorting technique in Java:First is internal sorting i.e that uses predefined sorting method ascending order Arrays.sort() for Primitive class arrays and wrapper class arrays and Collections.sort() for collections both methods sort the elements in ascendin
4 min read
Java Program to Show the Nesting of Methods
In java, the methods and variables which we create in a class can only be called by using the object of that class or, in case of static methods, we can directly call it by using the name of the class. The methods and variables can be called with the help of the dot operator. But there is a special
5 min read
How to Override toString Method for ArrayList in Java?
Every class in java is a child of Object class either directly or indirectly. toString() is present in Object class. The toString method returns a string representation of an object. The toString() can be overridden as part of a class to cater to the customized needs of the user. Whenever we try to
2 min read
Access Super Class Methods and Instance Variables Without super Keyword in Java
A class describes the property and the attributes of the object. Â A class contains mainly includes the following components. Modifiers: The keywords in java which provide a set of restriction on the class and its members and methods.Class keyword: The initialization of the class is done by class res
3 min read
Can We Define a Method Name Same as Class Name in Java?
We can have a method name same as a class name in Java but it is not a good practice to do so. This concept can be clear through example rather than explanations. In the below example, a default constructor is called when an object is created and a method with the same name is called using obj.Main(
3 min read
The @Override Annotation in Java
The @Override annotation is a standard Java annotation that was first introduced in Java 1.5. The @Override annotation denotes that the child class method overrides the base class method. For two reasons, the @Override annotation is useful. If the annotated method does not actually override anything
3 min read
Java Program to Show Inherited Constructor Calls Parent Constructor By Default
In java, there exists a very important keyword known as super() keyword in java which is widely used in java being object-oriented and hence inheritance comes into play. So whenever we use super keyword inside a child constructor then it calls the default parent constructor by itself. Example 1 Java
2 min read
Java Object Oriented Programming - Exercises
Looking for Java OOP exercises to test and improve your object-oriented programming skills? Explore our topic-wise Java OOP practice exercises, featuring over 25 practice problems designed to help you master key OOP concepts such as encapsulation, inheritance, polymorphism, and abstraction. Java is
15+ min read
Four Main Object Oriented Programming Concepts of Java
Object-oriented programming generally referred to as OOPS is the backbone of java as java is not a purely object oriented language but it is object oriented language. Java organizes a program around the various objects and well-defined interfaces. There are four pillars been here in OOPS which are l
7 min read