final
keyword in a method
declaration to indicate that the method cannot be overridden
by subclasses. The Object
class does this—a number
of its methods are final
.
You might wish to make a method final if it has an implementation
that should not be changed and it is critical to the consistent
state of the object. For example, you might want to make the
getFirstPlayer
method in this
ChessAlgorithm
class final:
class ChessAlgorithm { enum ChessPlayer { WHITE, BLACK } ... final ChessPlayer getFirstPlayer() { return ChessPlayer.WHITE; } ... }
Note that you can also declare an entire class final this
prevents the class from being subclassed. This is particularly
useful, for example, when creating an immutable class like the String
class.