Java Made Simple: Java final method overriding with Example

Java final Method : final keyword can be applied to methods apart variables and classes. The main advantage of declaring a method final is to prevent subclass to override.

Let us go through a simple program on final method.

class Test
{
  public final void display()		// it is final method and cannot be overridden
  {
    System.out.println("From super class final display() method");
  }
  public void show()		        // it is non-final method and can be overridden
  {
    System.out.println("From super class non-final show() method");
  }
}
public class Demo extends Test
{
  // public void display() {  }         // gives error, see the next screenshot
  public void show()
  {
    System.out.println("From subclass show() method");
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
    d1.display();                       // calls super class display()
    d1.show();		                // calls subclass show();    see the next screenshot
  }
}

1. Screenshot when the above example is executed

final method

2. Screenshot when the comment is removed in subclass for public void display()

final method

From above error message, you can understand that Java does not permit to override final methods.

Why final methods cannot be overridden?

Salient points to note with final methods

  1. Programmer declares a method as final in a class to prevent any subclass to override it. This is done by the Programmer when he would like to fix the behaviour of a method (else, it can be changed by subclass by overriding).
    1. There is a small discussion exists whether declaring a method final increases the performance or efficiency.

    2. Some Programmers say, does not increase because the method is loaded at runtime and thereby compilers cannot make any inline efficiency implementation.
    3. Every opportunity exists for the compiler to make final methods inline as compiler is well aware of the method is final (at compile time itself). To increase the performance, the final methods can be put inline or place in a cache as compiler knows very well they cannot be changed in entire life time and whole application. JRE and JIT are efficiently designed to do so.
  2. Making a method final is an indication to compiler not to allow method overriding.
  3. A final method cannot be hidden from subclass. That i, subclass cannot override, but can make use of it as it is visible from subclass. This is where final method differs from static method.
  4. final method prevents subclass to change the behaviour of super class method unexpectedly where the code is sensitive and crucial with some fixed functionality. Final methods are so designed to give complete functionality.
  5. A final method is deemed to be tried and tested so that subclass can use it straight away.
  6. Make method final for when authentication and authorization code is final and should never be altered.
  7. Inherited class cannot override the super class final method because the meaning and purpose is not to override.
  8. It is a compile-time error if the final method is overridden.
  9. You cannot hide final methods but you can hide private methods. This is where, final differs with private.
  10. In the Template design pattern, template methods are declared final.
  11. No meaning to make a private method final also. There is no added advantage.
  12. final methods cannot be overridden but can be overloaded.
  13. final methods can be inherited. Can be called with subclass object.
  14. abstract methods cannot be final and an abstract class cannot be final.
  15. A constructor cannot be final as constructor is not inherited.
  16. final methods are static binded (at compile time).

Finally, the ultimate purpose of final is to prevent overriding by subclass.

Leave a Comment

Your email address will not be published.