You have already seen the final modifier used in declaring constants. You may occasionally want to prevent classes from being extended. In such cases, use the final modifier to indicate that a class is final and cannot be a parent class. The Math class, introduced in Chapter 5, "Methods," is a final class. The String and StringBuffer classes, introduced in Chapter 8, "Strings," are also final classes.
You also can define a method to be final; a final method cannot be overridden by its subclasses.
Note
The modifiers are used on classes and class members (data and methods), except that the final modifier can also be used on local variables in a method. A final local variable is a constant inside a method. |