Final Classes

Making a data member or method final means that the data member or method cannot be overridden in a subclass or modified in any way at runtime. The same holds true for classes. A class that is declared as final cannot be subclassed or overridden. This again serves as a security feature in Java. No one can modify my CreditCard or Login class. The class String was made final because of several nefarious hacker tricks that could be used if Strings were not final. It is more common to make data members final than it is to make classes final, but you can find uses for this in class declarations.

Get PURE Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.