Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

October 30, 2015 Categories: Inheritance. No Comments on Method Overriding

In this article we will look at method overriding which is similar to method overloading. Method overriding is a mechanism which supports polymorphism in Java.

What is method overriding?

In the context of inheritance, suppose a base class A contains a method display with zero parameters and sub class B also contains a method display with zero parameters, what happens when we create an object for class B and call the display method using that object?

The method that will execute is the display method in the sub class B. Then what happened to the display method in super class A? It was hidden. This process of sub class method hiding the super class method when both methods contains same method signature is known as method overriding.

When is method overriding possible?

Method overriding is possible only when the following things are satisfied:

  • A class inherits from another class (inheritance).
  • Both super class and sub class should contain a method with same signature.

Note: Method overriding does not depend up on the return type of the method and the access specifiers like (public, private, protected etc..).

What is the difference between method overloading and method overriding?

Both may look similar but they are quite different in the following ways:

  • Method overloading takes place when a class contains multiple methods with the same name but varying number of parameters or types of parameters.
  • Method overriding takes place when two or more classes contains a method with same signature and all of them are participating in inheritance.

Now let’s look at some examples which demonstrates method overriding.

 

Example 1:

In the above example the method display in both classes A and B have same method signature (method name + number of parameters). So, the display method in class B hides the display method in class A. Output of the above program is:

This is display method of class B

Now let’s look at another example.

 

Example 2:

In the above example, even though there are methods with the same signature, as the object obj belongs to class A, the display method of class A is executed and the output is:

This is display method of class A

Now let’s consider another example.

 

Example 3:

In the above example, notice that the display method in the sub class accepts a single integer parameter. Now when we call display method as obj.display() in the Driver class, method overloading takes place and display method of class A is executed. Output of the above program is:

This is display method of class A

Now let’s consider another example.

 

Example 4:

In the above example, the display method in the super class A has been declared as a static. You have to remember that static methods cannot be overridden. So, the above program gives a compile time error.

 

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me