
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
We can prevent method overriding in Java in 3 ways
- By making method final in the base class
- By making a method static in the base class
- By making a method private in the base class
Final methods can not be overridden
By making a method final we are adding a restriction that derived class cannot override this particular method.
Example
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 can not 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 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
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 can not be overridden
Private methods of the base class are not visible in a derived class, hence they cannot be overridden.
Example
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
- Related Articles
- Using final keyword to Prevent Overriding in Java
- Method overriding in Java
- overriding method different package in java
- When Method Overriding occurs in Java?
- Rules for Java method overriding
- How many ways to iterate a TreeSet in Java?
- How many ways to iterate a LinkedList in Java?
- method overriding with access modifiers in Java
- Exception handling with method overriding in Java.
- Difference between Method Overloading and Method Overriding in Java
- java access modifiers with method overriding
- How many ways to synchronize an ArrayList in Java?\n
- Can we change method signature in overriding in Java?
- How many ways are there to register a driver in Java?
- How many ways to call garbage collector (GC) in Java?\n
