A final class that’s declared final
cannot be a superclass (i.e., a class cannot extend a final
class). All methods in a final
class are implicitly final
. Class String
is an example of a final
class. If you were allowed to create a subclass of String
, objects of that subclass could be used wherever String
s are expected. Since class String
cannot be extended, programs that use String
s can rely on the functionality of String
objects as specified in the Java API. Making the class final
also prevents programmers from creating subclasses that might bypass security restrictions. For more insights on the use of keyword final
, visit
docs.oracle.com/javase/tutorial/java/IandI/final.html
and
www.ibm.com/developerworks/java/library/j-jtp1029.html
Common Programming Error G.5
Attempting to declare a subclass of a final class is a compilation error.
Software Engineering Observation G.9
In the Java API, the vast majority of classes are not declared not declared final. This enables inheritance and polymorphism. However, in some cases, it’s important to declare classes final—typically for security reasons.