How to Prevent Method Overriding in Java

In this artcile we will discuss how to prevent method overriding in Java.

By preventing method overriding in Base class, we means that any class that extends this Base class will not able to override these methods.  Need of preventing method overriding is dicussed in next article.

Preventing Method Overriding in Java

Method overriding works because of 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 Derived class.

We can prevent method overriding in java in 3 ways i.e.

  1. By making method final in Base class
  2. By making method static in Base class
  3. By making method private in Base class

Let’s discus them one by one i.e.

Final methods can not be overridden

By making a method final we are adding a restriction that Derived class can not override this particular method i.e. checkout following example,

package com.thispointer.method.overriding.example2;

class Base {
	public void fun() {
		System.out.println("Base.fun()");
	}

	public final void test() {
		System.out.println("Base.test()");
	}
}

class Derived extends Base {

	@Override
	public void fun() {
		System.out.println("Derived.fun()");
	}

	// can not override test() method because its
	// final in Base class
	/*
	 * public void test() { System.out.println("Derived.test()"); }
	 */
}

public class Example {

	public static void main(String[] args) {

		Base ref = new Derived();

		// Calling the final method test()
		ref.test();
		
		// Calling the overridden method fun()
		ref.fun();

	}

}

Output:

Base.test()
Derived.fun()

Here Derived class can override fun() method because that non final in Base class but it cannot override test() method because that’s final in Base class.

Static methods can not be overridden in Java

We can not override the static methods in Derived class because Static function are linked with Class not with object. It means when we call a static method then JVM does not pass the this reference to it, like it does for all non static methods. So, static method doesn’t have a this reference avialable. Therefore run-time binding cannot take place for static methods. For static methods in Java, static binding takes place. Hence we can not override static methods in Java. Checkout this example,

package com.thispointer.method.overriding.example3;

class Base {
	public void fun() {
		System.out.println("Base.fun()");
	}

	static public void test() {
		System.out.println("Base.test()");
	}
}

class Derived extends Base {

	@Override
	public void fun() {
		System.out.println("Derived.fun()");
	}

	// This is not an overridden method, this will be
	// considered as new method in Derived class
	static public void test() {
		System.out.println("Derived.test()");
	}

}

public class Example {

	public static void main(String[] args) {

		Base ref = new Derived();
		// Calling the static method test()
		// It will call the Base class's test() because
		// it had static binding.
		ref.test();

		// Calling the overridden method fun()
		ref.fun();

	}

}

Output:

Base.test()
Derived.fun()

Private methods can not be overridden

Private methods of Base class are not visible in Derived class, hence they can not be override. Checkout following example,

package com.thispointer.method.overriding.example4;

class Base {
	public void fun() {
		System.out.println("Base.fun()");
	}

	private void test() {
		System.out.println("Base.test()");
	}
}

class Derived extends Base {

	@Override
	public void fun() {
		System.out.println("Derived.fun()");
	}

	// This is not an overridden method, this will be
	// considered as other function
	public void test() {
		System.out.println("Derived.test()");
	}

}

public class Example {

	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 fun()
		ref.fun();

	}

}

Output:

Derived.fun()

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top