What happens if we try to extend a final class in java?

Java 8Object Oriented ProgrammingProgramming

In Java final is the access modifier which can be used with a filed class and a method.

  • When a method if final it cannot be overridden.
  • When a variable is final its value cannot be modified further.
  • When a class is finale it cannot be extended.

Extending a final class

When we try to extend a final class that will lead to a compilation error saying “cannot inherit from final SuperClass”


In the following Java program, we have a final class with name SuperClass and we are trying to inherent it from another class (SubClass).

final class SuperClass{
   public void display() {
      System.out.println("This is a method of the superclass");
public class SubClass extends SuperClass{
   public static void main(String args[]){
      //Calling method of the superclass
      new SubClass().display();

Compile time error

On compiling, this program generates a compilation error as shown below −


SubClass.java:7: error: cannot inherit from final SuperClass
public class SubClass extends SuperClass{
1 error
Published on 03-Jul-2019 15:05:33