Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Writing Final Classes and Methods

Final Classes

Java allows you to declare that your class is final; that is, that your class cannot be subclassed. There are (at least) two reasons why you might want to do this: security reasons and design reasons.

Security: One mechanism that hackers use to subvert systems is to create subclasses of a class and then substitute their class for the original. The subclass looks and feels like the original class but does vastly different things possibly causing damage or getting into private information. To prevent this kind of subversion, you can declare your class to be final and prevent any subclasses from being created. The String class in the java.lang package is a final class for just this reason. The String class class is so vital to the operation of the compiler and the interpreter that the Java system must guarantee that whenever a method or objects uses a String they get exactly a java.lang.String and not some other string. This ensures that all strings have no strange, inconsistent, undesirable or unpredictable properties.

If you try to compile a subclass of a final class, the compiler will print an error message and refuse to compile your program. In addition, the bytecode verifier ensures that the subversion is not taking place at the bytecode level by checking to make sure that a class is not a subclass of a final class.

Design: Another reason you may wish to declare a class as final are for object-oriented design reasons. You may think that your class is "perfect" or that, conceptually, your class should have no subclasses.

To specify that your class is a final class, use the keyword final before the class keyword in your class declaration. For example, if you wanted to declare the ImaginaryNumber class as final, its declaration would now look like this:

final class ImaginaryNumber extends Number implements Arithmetic {
    . . .
}
Note that it doesn't make sense for a class to be both final and abstract. In other words, a class that contains unimplemented methods cannot be final. Attempting to declare a class as both final and abstract results in a compile-time error.

Any subsequent attempts to subclass ImaginaryNumber will result in a compiler error such as the following:

Imagine.java:6: Can't subclass final classes: class ImaginaryNumber
class MyImaginaryNumber extends ImaginaryNumber {
      ^
1 error

Final Methods

If creating a final class seems heavy handed for your needs, and you really just want to protect some of your classes methods from being overriden, you can use the final keyword in a method declaration to indicate to the compiler that the method cannot be overridden by subclasses.

You might wish to make a method final if the method has an implementation that should not be changed and is critical to the consistent state of the object. For example, [PENDING: example]


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces