Overridable methods need special care
Any method which is not 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 is possible for a subclass to break its superclass's contract. See Effective Java for more information.
If you do not 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.
See Also :
Use final liberally
Designing for subclassing
Constructors shouldn't call overridables
Use @Override liberally
Designing for subclassing
Constructors shouldn't call overridables
Use @Override liberally
Would you use this technique?
|
|