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