This discussion is archived
3 Replies Latest reply: Feb 12, 2013 3:02 PM by rp0428 RSS

About Final class

990610 Newbie
Currently Being Moderated
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.
  • 1. Re: About Final class
    rp0428 Guru
    Currently Being Moderated
    >
    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
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.1.2
    >
    8.1.1.2. 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 (§8.1.1.1).

    Because a final class never has any subclasses, the methods of a final class are never overridden (§8.4.8.1).
  • 2. Re: About Final class
    990610 Newbie
    Currently Being Moderated
    ok...fine.
    Nice answer for this question. Thanks
  • 3. Re: About Final class
    rp0428 Guru
    Currently Being Moderated
    Please mark your question ANSWERED if it has been.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points