As we know, we can't inherit final class in java but I want to know why java doesn't allow to do so? What is the reason behind it?
-
What else would be the purpose of the final keyword? – Ferrybig Feb 08 '16 at 08:16
-
1@Ferrybig I believe the question is "why would anyone want to disallow inheritance, ever", not "why does 'final' disallow inheritance". – molbdnilo Feb 08 '16 at 08:20
-
final keyword was created so that the class cannot be extended not the other way around. – Aniket Thakur Feb 08 '16 at 08:31
4 Answers
final
specifically means "do not inherit me" (in this context, it has other meaning in different contexts).
The reason for developers to mark the class as final are mainly optimizations. Either they can guarantee that some variables will be specifically of some class and also JIT can take advantage of this as it can avoid more expensive virtual calls and rather inline the methods directly.

- 18,186
- 3
- 35
- 43
Because that was the objective of final keyword, i.e., to make the class not to be inherited. You can read this article:
Benefits of final keyword in Java
Here are few benefits or advantage of using final keyword in Java:
Final keyword improves performance. Not just JVM can cache final variable but also application can cache frequently use final variables.
Final variables are safe to share in multi-threading environment without additional synchronization overhead.
Final keyword allows JVM to optimized method, variable or class.

- 168,305
- 31
- 280
- 331
I think it is a reasonable request. Of course, the subclass shouldn't be able to override any behavior of the final
super class. But the subclass can add its own, independent state and behavior. That is, we could weaken the final class
semantics to mean all it methods are final
, but the class can still be subclassed. This feature could become useful in some cases.

- 19,446
- 5
- 33
- 61
Many OOP languages have this feature, C# for instance uses "sealed" to accomplish the same thing. In addition to the aforementioned compiler optimisations, it is also useful for enforcing certain constraints on the consumers of (e.g.) a class library where it is undesirable to allow the client code to override certain aspects of the libraries functionality for either security or protection of intellectual property reasons.