
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How many ways to prevent method overriding in Java?
Method overriding works because of the run-time method binding feature in Java. So, if we force the Java compiler to do static binding for a method then we can prevent that method from being overridden in a derived class.
Preventing Method Overriding in Java
We can prevent method overriding in Java in 3 ways:
- By declaring a method as "final" in the base class
- By declaring a method as "static" in the base class
- By declaring a method as "private" in the base class
Final Methods cannot be Overridden
By declaring a method as final, we are restricting the child classes from overriding it.
Example
In this Java program, the test() method is final, so it cannot be overridden. Doing so will generate an error.
class Base { public void show() { System.out.println("Base class show() method"); } public final void test() { System.out.println("Base class test() method"); } } class Derived extends Base { public void show() { System.out.println("Derived class show() method"); } // can not override test() method because its final in Base class /* public void test() { System.out.println("Derived class test() method"); } */ } public class Test { public static void main(String[] args) { Base ref = new Derived(); // Calling the final method test() ref.test(); // Calling the overridden method show() ref.show(); } }
Output:
Base class test() method Derived class show() method
Static Methods cannot be Overridden
We can not override the static methods in a derived class because static methods are linked with the class, not with the object. It means when we call a static method, then the JVM does not pass this reference to it as it does for all non-static methods. Therefore, run-time binding cannot take place for static methods.
Example
Here we are preventing method overriding by defining the method as static.
class Base { public void show() { System.out.println("Base class show() method"); } public static void test() { System.out.println("Base class test() method"); } } class Derived extends Base { public void show() { System.out.println("Derived class show() method"); } // This is not an overridden method, this will be considered as new method in Derived class public static void test() { System.out.println("Derived class test() method"); } } public class Test { public static void main(String[] args) { Base ref = new Derived(); // It will call the Base class's test() because it had static binding ref.test(); // Calling the overridden method show() ref.show(); } }
Output:
Base class test() method Derived class show() method
Private Methods cannot be Overridden
Private methods of the base class are not visible in a derived class, hence, they cannot be overridden.
Example
The following Java program shows how to prevent method overriding by defining it as private.
class Base { public void show() { System.out.println("Base class show() method"); } private void test() { System.out.println("Base class test() method"); } } class Derived extends Base { public void show() { System.out.println("Derived class show() method"); } // This is not an overridden method, this will be considered as other method. public void test() { System.out.println("Derived class test() method"); } } public class Test { public static void main(String[] args) { Base ref = new Derived(); // Cannot call the private method test(), this line will give compile time error // ref.test(); // Calling the overridden method show() ref.show(); } }
Output:
Derived class show() method