Java Method Overriding

(Last Updated On: March 6, 2019)

Method overriding java examples using base and child class relationship, interface and abstract class with explanation.

We’ll learn how a child class can use base class behaviors / methods and override some of them in child class if don’t want to use base class methods without removing the methods from base class.

[Method Override definition– Writing the method in child class with same name, signature and return type, which is also in base class is known as method overriding. The child class can write its definition different that base class same method.]

Prerequisite:

Let’s understand method overriding in java by example…

Let’s say a son is using his father’s home and car and he is using his own mobile. See the below program (No overriding concept) that implements this scenario. After that we will see the method overridden code.

The output of this java program will be as below

Output:

Father’s Home
Father’s Car
Son’s mobile

 

 

Java code using method overriding

At later point of time, Son no longer wants to use his father’s Car. But, he still wants to use father’s home.

As a solution, Son will override Car() method and implement for own. So, the Car method with same name will be written in Son class and implemented. This is in fact method overriding concept.

Now the output will be as below. Notice here Son’s Car is called. In previous example it was Father’s Car.

Output:

Father’s Home
Son’s Car
Son’s mobile

 

 

DOUBT ON METHOD OVERRIDING

Doubt arises in mind that if we are going to write the same method and implement in child class, then why don’t we delete that method from the base class as we will no longer use that method from parent class.

Lets think of above program example, some later point of time, Son wanted to use his own Car and not his Father’s car. So, he overridden the Car method. Now answer yourself- Should the Son destroy his Father’s Car?

Off course not, some other child might be using that.

Similarly, You might use some third party base class, where you may not allow to change the class. Secondly, the parent class methods may be being used by other classes in the project/ applications that will not work.

So, It is not a good idea to remove method from a base class when you override that in child a class.

 

Let’s see another example i.e. a class implementing an interface , where you don’t have choice but implement and override the method. You can see an example of interface in java programming.

Prerequisite:

Method overriding java example using interface

In below java program, there is an interface Adapter. The class LaptopAdapter will implement the interface. The class will override the methods of interface and implement it.

Note that if a class is implementing an interface, then the class must override and define it in the class itself or else compiler will flash compile time error.

 

Output:
Adapter plugged in

 

Example of Method Overriding in Java with Abstract Class

This is another example of overriding an abstract method of an abstract class. In below java program the abstract method engine() of Car class is being overridden in sub class Maruti.

In main() program, we have created an object of Maruti and call engine and run methods.

Output
1000 cc
Car is running…

NOTE:

Method overriding concept comes under run time polymorphism in java oops. Whereas, java constructor overloading and method overloading in java programming comes under compile time polymorphism.  You can read an interesting interview question – why method overriding is called run time polymorphims.