Skip to content
Related Articles

Related Articles

Improve Article

Different Ways to Prevent Method Overriding in Java

  • Last Updated : 27 Jan, 2021

Inheritance is a substantial rule of any Object-Oriented Programming (OOP) language but still, there are ways to prevent method overriding in child classes which are as follows:

Methods:

  1. Using a static method
  2. Using private access modifier
  3. Using default access modifier
  4. Using the final keyword method

Method 1: Using a static method

This is the first way of preventing method overriding in the child class. If you make any method static then it becomes a class method and not an object method and hence it is not allowed to be overridden as they are resolved at compilation time and overridden methods are resolved at runtime.

Java




// Java Program to Prevent Method Overriding
// using a static method
 
// Importing java input output classes
import java.io.*;
 
// Class 1
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object og Base class
        Base base = new Child();
 
        // Printing message from base class as
        // its static methods have static binding
 
        // Hence even if the object is of Child class
        //  message printed is from base class
        base.hello();
    }
}
 
// Class 2
// Parent class
class Base {
 
    // hello() method of parent class
    public static void hello()
    {
 
        // Print and display the message if
        // hello() method of parent class is called
        System.out.println("Hello from base class");
    }
}
 
// Class 3
// Child class
class Child extends Base {
 
    // Overriding the existing method - hello()
    // @Override
    // hello() method of child class
    public static void hello()
    {
        // Print and display the message if
        // hello() method of child class is called
        System.out.println("Hello from child class");
    }
}
Output



Hello from base class

Method 2 Using private access modifier
 
Making any method private reduces the scope of that method to class only which means absolutely no one outside the class can reference that method.

Example

Java




// Java Program to Prevent Method Overriding
// using a private method specifier
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of Child class
        Child child = new Child();
 
        // Calling hello() method in main()
        child.hello();
    }
}
 
// Class 2
// Helper class 1
// Child Class
class Child extends Base {
 
    //@Override
    // hello() method of child class
    public void hello()
    {
 
        // Print statement when hello() method of
        // child class is called
        System.out.println("Hello from child class");
    }
}
 
// Class 3
// Helper class 2
// Parent Class
class Base {
 
    // hello() method of parent class
    private void hello()
    {
 
        // Print statement when hello() method of
        // child class is called
        System.out.println("Hello from base class");
    }
}
Output
Hello from child class

Method 3 Using default access modifier

This can only be used when the method overriding is allowed within the same package but not outside the package. Default modifier allows the method to be visible only within the package so any child class outside the same package can never override it.
 

Example 

Java




// Java Program to Prevent Method Overriding
// using a private method specifier
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of base class
        Base base = new Child();
 
        // Calling hello() method using Base class object
        base.hello();
    }
}
 
// Class 2
// Parent class
class Base {
 
    // hello() method of parent class
    private void hello()
    {
 
        // Print statement when hello() method of
        // child class is called
        System.out.println("Hello from base class");
    }
}
 
// Class 3
// Child class
class Child extends Base {
 
    // Overriding existing method
    // @Override
 
    // Hello method of child class
    void hello()
    {
 
        // Print statement when hello() method
        // of child class is called
        System.out.println("Hello from base class");
    }
}

Output:

 
Method 4: Using the final keyword method

The final way of preventing overriding is by using the final keyword in your method. The final keyword puts a stop to being an inheritance. Hence, if a method is made final it will be considered final implementation and no other class can override the behavior.

Java




// Java Program to Prevent Method Overriding
// using a final keyword method
 
// Importing input output classes
import java.io.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of Child class
        Child child = new Child();
 
        // Calling hello() method using Child class object
        child.hello();
    }
}
 
// Class 2
// Child class
class Child extends Base {
 
    // Overriding
    // @Override
 
    // Method of child class
    public void hello()
    {
        // Print statement for Child class
        System.out.println("Hello from child class");
    }
}
 
// Class 3
// Base class
class Base {
 
    // Method of parent class
    public final void hello()
    {
        // Print statement for Base(parent) class
        System.out.println("Hello from base class");
    }
}

Output:

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 :