Method overriding in Java – 4 real time examples

(Last Updated On: July 15, 2018)

Learn method overriding in java language with 4 real time examples. All examples are simulated with working pseudo programs and explained scenario.

Method overriding in java oops is run time polymorphism and is always implemented in inheritance relationship. Inheritance relationship can be in between base class and derived class or between interface and its sub classes in java programming.

In below image, we can see how we override methods of a base class in to a child class. And, also override method of an interface by implementing in sub class.

method overriding in java

Method overriding feature gives ability to a child class to define its own behavior if It does not want to use parent class behavior.Meaning, a child class can write a method with same name that is also in base class and implement it. it is called method overriding in java oops.

 

Method overriding in java with simple example

Lets consider an example that, A Son inherits his Father’s public properties e.g. home and car and using it. At later point of time, he decided to buy and use his own car, but, still he wants to use his father’s home. So, what he can do?

He can use method overriding feature and use his own car.  See below example, how he has overridden his own car method. He is not supposed to destroy his father car. right? Once you create the object of Son and call home and car methods. Father’s home and Son’s car will be called.

 

Example of method overriding using Parent class and child

 

 

Output:
Father’s home
Son’s Car

 

So for, we understand that a child class can override a method of a parent class and can give its own definition.

Similarly, if we have an interface, the class who inherit and implement the interface, class must override all methods of the interface.

In below class we have Shape interface with a draw () method.  The class Circle will override the method of interface and implement it.

Note that an interface has only abstract methods that is not implemented or say does not have definition or implementation. You can read here complete tutorial on interface in java with example.

 

Example of method overriding in java using Parent Interface and subclass

 

Output:
Circle…

 

 

As a conclusion,

  • Don’t want to use some of the base class methods? Use feature method overriding in java programs. Write those methods with same name in your class and implement them. Whenever you call those methods with your objects then your methods will be called or else base class method will be called.
  • Do you have an interface and want to use all its methods to be implemented? Create a class, inherit the interface using implements keyword and override all its methods and implement it in the class.

 

 

Now let’s read some simulated real-life examples where method overriding is required. All pseudo code is extracted from real life projects.

Scenarios when method overriding in java oops is required

  • Change Requirements
  • Need contracts in Interface and abstract classes
  • Code extendibility
  • Achieve loose coupling

 

Example -1: Using method java method overriding on Change Requirement

Method overriding in java when change requirements occur – There are two music companies Sony and Panasonic. They want to use a third-party music library. They are using all operations (methods) from Music class. To make the program simple and understandable, let’s say Music class provides music initialization and one of the methods i.e. play() method.

Both Sony and Panasonic companies using play method of Music class. After years, Sony decided that all the methods from the music class will be used but it will no longer use the play method for some reason. But, Panasonic and other companies want to use the library’s play method.

So, Sony overrides the play method and provide its own implementation. Whereas other companies continue using the paly method of Music class from third party. so, they don’t override it.

 

 

 

Example -2: Java method overriding on need of contracts in Interface and abstract classes

  1. a) Example of overriding interface methods.

Let’s say Samsung company requested Dell company to provide support for Samsung mouse to Dell laptops.

Dell agreed on that and ask Samsung to implement his contracts to give support as per their design. Dell gives contracts in an interface to Samsung as given below.

 

 

Now Samsung will implement these contracts. Meaning, mouse classes will inherit the interface by using implements keyword and override all methods and implement them in the class. for example

 

 

 

b) Example of overriding abstract methods. – Vending machine design

 

 

 

Example-3: Method overriding in java oops on Code extendable

Method overriding in java applications to make code extendable – Consider a Game that comprises many scenes like house, bus stop and supper market etc. As a good design, scenes should be extendable. So, we can create an interface Scene and extend multiple types of scenes. All scenes i.e. house and super market will override and implement the interface methods.

for example, in below java source code. House and SuperMarket class have override and implemented display() method of interface.

 

 

Output:
House Scene
Super Market Scene

 

 

Example-4: Java method overriding to achieve loose coupling example

Method overriding in java to achieve loose coupling – Consider a very complex existing manager class that is handling multiple types of working. We might not be interested to modify the manager class again and again whenever we must add a new types workers time to time. We know that in real time software product, even though we change a little in existing class, entire class need to be tested.

Note that loose coupling means reducing dependencies of a class that use different class directly. Tight coupling means classes and objects are dependent on one another.

In given scenario, if Manage class compose multiple types of workers then it would be tightly coupled code and when new type of worker is required, we must change the class. It is better to have an interface and let all types of workers implement and override its methods. Read comparison example of tight coupling and loose coupling.