Java Reference
In-Depth Information
The
final
Modifier
You can mark a method to indicate that it cannot be overridden with a new definition
in a derived class. Do this by adding the
final
modifier to the method heading, as in
the following sample heading:
final
public final void
someMethod()
{
.
.
.
An entire class can be declared final, in which case you cannot use it as a base class
to derive any other class from it. The syntax for declaring a class to be final is illustrated
in what follows:
public final class
SomeClass
{
.
.
.
If a method is marked as
final
, it means the compiler can use early binding with
that particular method, which enables the compiler to be more efficient. However, the
added efficiency is normally not great, and we suggest not using the
final
modifier
solely for reasons of efficiency. (Also, it can sometimes aid security to mark certain
methods as
final
.)
You can view the
final
modifier as a way of turning off late binding for a method
(or an entire class). Of course, it does more than just turn off late binding—it turns off
the ability to redefine the method in any descendent class.
The
final
Modifier
If you add the modifier
final
to the definition of a method, it indicates that the method
may not be redefined in a derived class. If you add the modifier
final
to the definition of
a class, it indicates that the class may not be used as a base class to derive other classes.