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'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.

See Also :
Use final liberally
Designing for subclassing
Standardized dialogs
Constructors shouldn't call overridables
Use @Override liberally
Would you use this technique?
Yes   No   Undecided   
© 2014 Hirondelle Systems | Source Code | Contact | License | RSS
Individual code snippets can be used under this BSD license - Last updated on September 21, 2013.
Over 2,000,000 unique IPs last year - Built with WEB4J.
- In Memoriam : Bill Dirani -