When We Need to Prevent Method Overriding in Java ?
Here we will be discussing why should we prevent method overriding in java. So, before going into the topic, let’s give a look at the below important concept then we will move to the actual topic. As we know Object Oriented Programming (OOPs) concept consists of 4 important concepts that are as follows:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Polymorphism is one of the important pillars among the 4 pillars of Oops which refers to performing a single action in different ways. This means the object behaves differently under different conditions. It is divided into 2 major types i.e.
- Compile-time polymorphism OR Static polymorphism OR Early binding.
- Run time polymorphism OR Dynamic polymorphism OR Late binding.
Both Method overloading and Method Overriding have some differences between them as well. While working practically on a project there arises some situations when we need to implement overloading or overriding as well at some scenarios we need to prevent overloading and overriding. As our topic aims to focus on ‘Why should we prevent method overriding ?’ let’s move to that part. While working on any project we avoid method overriding in different ways mostly for two reasons listed and further discussed below:
- Performance parameter
- Design decision
Let us discuss and implement them by going through them to a certain depth.
Reason 1: Performance Parameter
In overriding JVM performs extra stuff at run time which affects performance. Yes, it’s method binding which means that method will be called is decided at run time based on the actual object pointed by reference, so it takes extra time whereas in static binding during compile time which method will be called is decided(function call binds with the appropriate method). So, it’s clear we are preventing overriding means we are preventing run-time binding and we are preventing run-time binding means we are saving time and increasing performance. So, sometimes we prevent overriding to improve the performance of software applications.
Reason 2: Design Decision
In many scenarios/situations, we do not want to change the behavior of particular base class methods by their derived classes. This means we want to keep the parent class method as it is and not be changed by its child class.
Implementation:
We have a Base class Car and a derived class Skoda Car where the Base class has 3 methods as listed below:
- speed()
- condition()
- maintenance()
Here we want the speed() method to remain the same as it is and the other two methods condition() method and maintenance() method to be changed(means body provided in derived class). it is as discussed in the below example.
Example
Java
import java.io.*;
class Car {
public final void speed()
{
System.out.println( "Maximum speed allowed" );
}
public void condition()
{
System.out.println( "good" );
}
public void maintenance()
{
System.out.println( "required" );
}
}
class Skoda_Car extends Car {
public void condition()
{
System.out.println( "Car condition good" );
}
public void maintenance()
{
System.out.println( "Maintenance not required" );
}
}
class Main {
public static void main(String[] args)
{
Skoda_Car c = new Skoda_Car();
c.speed();
c.condition();
c.maintenance();
}
}
|
Output
Maximum speed allowed
Car condition good
Maintenance not required
Last Updated :
07 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...