Different Ways to Prevent Method Overriding in Java

10 Aug, 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:
Using a static methodUsing private access modifierUsing default access modifierUsing the final keyword method
Using a static method
Using private access modifier
Using default access modifier
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 classesimport java.io.*; // Class 1// Main classclass 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 classclass 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 classclass 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");    }}
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 classesimport java.io.*; // Class 1// Main classpublic 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 Classclass 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 Classclass 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");    }}
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 classesimport java.io.*; // Class 1// Main classclass 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 classclass 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 classclass 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 child 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 classesimport java.io.*; // Classclass 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 classclass 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 classclass Base {     // Method of parent class    public final void hello()    {        // Print statement for Base(parent) class        System.out.println("Hello from base class");    }}
Output:
RajSuvariya
prabormukherjee
java-inheritance
Java-Object Oriented
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.