Final Classes


A final class is one that cannot be subclassed. A final class is designated by the final keyword in the class definition syntax. There are two general reasons for creating a final class.

  1. As a security mechanism. Because of inheritance, a method that takes an object as an input parameter can be called using a subclass argument instead. Using subclasses is one technique hackers use to subvert systems. The subclass may share the common features of its parent but also be written to do something nasty like altering or accessing sensitive information. Code that contains a subclass of a final class will not compile.

  2. You may wish to shut off the possibility of inheritance for a class. An example of a final class in the Java API is the String class. Because strings are so pervasive and so important to Java's operation, it's important that the function of the String class not be altered from the language specifications.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net