This content has been marked as final. Show 3 replies
In java core API , we will found that the String class, StringBuilder class, and many other classes are declared as final.So my doubt is, why the String and StringBuilder class are made final ?In which scenario for an application, i can just choose a class to make as final ? please help me .Thanks.
They are made final so that you cannot override and extend them to make them mean something else.
See the Java Language Spec
18.104.22.168. final Classes
A class can be declared final if its definition is complete and no subclasses are desired or required.
It is a compile-time error if the name of a final class appears in the extends clause (§8.1.4) of another class declaration; this implies that a final class cannot have any subclasses.
It is a compile-time error if a class is declared both final and abstract, because the implementation of such a class could never be completed (§22.214.171.124).
Because a final class never has any subclasses, the methods of a final class are never overridden (§126.96.36.199).