How to Prevent Method Overriding In Java Inheritance?

There are three ways to stop method overriding in Java inheritance. Final, static, and private methods cannot be overridden by a subclass. If a method is declared final, static or private in base class, it cannot be overridden by the subclass. Here, we will see all three ways of preventing method overriding with help of suitable examples.

1. Using final keyword to prevent method overriding during inheritance:
All methods and variables can be overridden by default in a subclasses. If we need to prevent the subclasses from overriding the members of the superclass, we can declare them final. Any attempt to override a final method in a subclass results in the compile-time-error.

Following code snippet demonstrates how final keyword prevents method overriding. When a method is declared as final then it cannot be overridden by subclasses.

class Parent {
    final void fun()
    {
        System.out.println("Parent Fun");
    }
}
 
class Child extends Parent{
    //Compiler error
    void fun()
    {
        System.out.println("Child Fun");
    }
}
 
public class Demo {
 
    public static void main(String[] args){
 
        Parent obj = new Child();
        obj.fun();
   }
}

Using final keyword to prevent inheritance:
When a class is declared as final then it cannot be subclassed i.e. no other class can extend it. This is particularly useful, for example, when creating an immutable class like the predefined String class. Declaring a class as final implicitly declares all of its methods as final, too. Also note that it is illegal to declare a class as both abstract and final since an abstract class is incomplete by itself and relies upon its subclasses to provide complete implementations.

class Parent {
    final void fun()
    {
        System.out.println("Parent Fun");
    }
}
//Compiler Error. Can't subclass a final class
class Child extends Parent{
 
    void fun()
    {
        System.out.println("Child Fun");
    }
}

2. static methods can not be overridden in Java:
We can not override static methods in subclass because static methodss are linked with class not with object. It means when we call a static method then JVM does not pass the this reference to it, like it does for all non-static methods. So, static method doesn’t have a this reference avialable. Therefore run-time binding cannot take place for static methods. For static methods in Java, static binding takes place. Hence we can not override static methods.

 
class Parent {
    public static void fun()
    {
        System.out.println("Parent Fun");
    }
}
 
class Child extends Parent {
 
    public static void fun()
    {
        System.out.println("Child Fun");
    }
}
 
public class Demo1 {
    static public void main(String [] args) {
        Parent obj = new Child();
        obj.fun();
    }
}
 
Output: Parent Fun

3. private methods can not be overridden in Java:
Private methods of base class are not visible in subclass, hence they can not be overridden.

 
class Parent {
    private void fun()
    {
        System.out.println("Parent Fun");
    }
}
 
class Child extends Parent {
    //Compiler Error: The method fun() of type Child must override or implement a supertype method
    @Override
    private void fun()
    {
        System.out.println("Child Fun");
    }
}
 
public class Demo1 {
    static public void main(String [] args) {
        Parent obj = new Child();
        //Compiler Error: The method fun() from the type Parent is not visible
        obj.fun();
    }
}
 

Hope you have enjoyed reading this post. Please do write us if you have any suggestion/comment or come across any error on this page. Thank you for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.