Open In App

Can We Override Final Method in Java?

Last Updated : 19 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A method in a subclass is said to Override a method in its superclass if that method has a signature identical to a method in its superclass. When this method is called by an object of the subclass, then always the Overridden version will be called. In other words, the method which was Overridden in the superclass will become hidden.

The JVM has 2 choices, it can either call the original method present in the superclass or the Overridden method present in the subclass. Due to late binding, this decision is taken at run-time and not at compile-time.

Can We Override a Final Method?

No, the Methods that are declared as final cannot be Overridden or hidden. For this very reason, a method must be declared as final only when we’re sure that it is complete.

  • It is noteworthy that abstract methods cannot be declared as final because they aren’t complete and Overriding them is necessary.
  • Methods are declared final in java to prevent subclasses from Overriding them and changing their behavior, the reason this works is discussed at the end of this article.
  • The advantage of the final keyword is that it stops developers from intentionally or unintentionally changing the behavior of methods that should not be changed for security or other reasons.

Result of Overriding a Final Method

In the code below:

  • The class IntegralOperations has two methods, add and subtract that perform the expected operation.
  • Both add and subtract are declared final.
  • The class child extends IntegralOperations and tries to Override add and subtract.

Java




// Java Program to demonstrate result of overriding a final
// method
  
class IntegralOperations {
    
    // add declared as final
    final int add(int a, int b) { return a + b; }
    
    // subtract declared as final
    final int subtract(int a, int b) { return a - b; }
}
  
class child extends IntegralOperations {
  
    // try to override add
    @Override int add(int a, int b) { return a - b; }
  
    // try to override subtract
    @Override int subtract(int a, int b) { return a * b; }
}
  
public class Main {
    public static void main(String[] args)
    {
        child c1 = new child();
        System.out.println(c1.add(1, 4));
    }
}


Output

prog.java:13: error: add(int,int) in child cannot override add(int,int) in IntegralOperations
    @Override int add(int a, int b) { return a - b; }
                  ^
  overridden method is final
prog.java:16: error: subtract(int,int) in child cannot override subtract(int,int) in IntegralOperations
    @Override int subtract(int a, int b) { return a * b; }
                  ^
  overridden method is final
2 errors

As evident from the above Output, attempting to Override a final method causes a compile-time error.
This proves that final methods cannot be overridden in Java.

Why Does The final Keyword Prevent Overriding?

In the beginning, it was discussed that methods to be Overridden follow Dynamic Method Dispatch (late binding)But the methods which are declared as final follow static binding (early binding), which means that the method definition will be grouped with a body at compile-time itself.

In other words, JVM must know exactly which method to call at compile-time.To accomplish this, for every final method definition there must be a unique body. But if Overriding is allowed then there can be multiple bodies for a method definition and then the compiler won’t be able to choose one of them.

This issue is seen in the above example where we have two bodies each for add and subtract methods. This prompts the compiler to throw an error at compile-time. In this way, the final keyword prevents Overriding and guards the semantics of a method.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads