Skip to content
Related Articles

Related Articles

Improve Article

When We Need to Prevent Method Overriding in Java ?

  • Last Updated : 11 Jul, 2021
Geek Week

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:

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

Polymorphism is one of the important pillars among the 4 pillars of Oops which refers to perform a single action in different ways. This means the object behaves differently at different conditions. It is divided into 2 major types i.e.

  1. Compile-time polymorphism OR Static polymorphism OR Early binding.
  2. 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 in 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 in any project we avoid method overriding in different ways mostly for two reasons listed and further discussed below:

  1. Performance parameter
  2. Design decision

Let us discuss and do 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 retain 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




// Java Program to illustatte Prevention method overriding
  
// Importing input output classes
import java.io.*;
  
// Class 1
// Base class
class Car {
  
    // We do not want spped to be changed change in base
    // class, so we will not provide any body to speed()
    // method in derived class So we will provide new body
    // to condition() method and maintenance() method in
    // derived class
  
    // Methods of this class
    public final void speed()
    {
  
        // Print statement
        System.out.println("Maximum speed allowed");
    }
    public void condition()
    {
        // Print statement
        System.out.println("good");
    }
    public void maintenance()
    {
        // Print statement
        System.out.println("required");
    }
}
  
// Class 2
// Derived class
class Skoda_Car extends Car {
    public void condition()
    {
  
        // Print statement
        System.out.println("Car condition good");
    }
  
    public void maintenance()
    {
  
        // Print statement
        System.out.println("Maintenance not required");
    }
}
  
// Class 3
// Main class
class Main {
  
    // Main drive method
    public static void main(String[] args)
    {
  
        // Here Parent class reference refers to
        // a Child object so child class methods will be
        // called This is known as run -time polymorphism
  
        // Creating an object of child class in Main class
        Skoda_Car c = new Skoda_Car();
  
        // Calling methods in Base class over object created
        // above
        c.speed();
        c.condition();
        c.maintenance();
    }
}
Output
Maximum speed allowed
Car condition good
Maintenance not required

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :