- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What happens if we try to extend a final class in java?
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”
Example
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 −
Output
SubClass.java:7: error: cannot inherit from final SuperClass public class SubClass extends SuperClass{ ^ 1 error
- Related Articles
- What happens when we try to add a duplicate key into a HashMap object in java?
- What will happen when we try to override final method of the superclass in Java?
- What happens if we call "super()" in a constructor without extending any class, in java?
- What happens when we try to add a number to undefined value?
- What happens when you declare a method/constructor final in Java?
- Final class in Java
- What is overloading? What happens if we overload a main method in java?
- Why we can't initialize static final variable in try/catch block in java?
- Make a class final in Java
- Can Enum extend any class in java?
- Can you extend a static inner class in Java?
- What happens if we define a concrete method in an interface in java?
- Effectively final variables in try-with-resources in Java 9?
- Can we extend interfaces in Java? Explain?
- Can we extend an enum in Java?

Advertisements