Use final liberally
Designing for subclassing
Standardized dialogs
Constructors shouldn't call overridables
Use @Override liberally
private
, static
, or final
can be overridden. Allowing a method to be overridden should always
be done intentionally, not by accident. Overridable methods, and any
methods which call them, represent unusual places in your code, to which
special attention must be paid. This is because subclassing violates encapsulation,
in the sense that it's possible for a subclass to break its superclass's
contract. See Effective
Java for more information.
If you don't intend a method to be overridden, then you should declare
it as private
, static
, or final
.
When you do override a method, you should use the @Override
annotation.
This allows the compiler to verify that the method is indeed a valid override of an existing method.
For example, your implementations of toString
, equals
, and hashCode
should always use @Override
in the method header.