Use @Override liberally

The @Override annotation was added in JDK 1.5. It functions more or less as a new method modifier. Its presence indicates to the compiler that the annotated method must override an existing superclass method.

The most common use case for @Override is with Object methods :

@Override public String toString(){...}
@Override public boolean equals(Object){...}
@Override public int hashCode(){...}
The main reason @Override was created was to deal with simple typographical errors. For example, a method mistakenly declared as
public int hashcode(){...}
is in fact not an override - the method name has all lower case letters, so it doesn't exactly match the name of the hashCode() method. It will however compile perfectly well. Such an error is easy to make, and difficult to catch, which is a dangerous combination. Using the @Override annotation prevents you from making such errors.

You should be in the habit of using @Override whenever you override a superclass method.

See Also :
Overridable methods need special care
Modernize old code
Would you use this technique?
Yes   No   Undecided   
© 2013 Hirondelle Systems | Source Code | Contact | License | RSS
Individual code snippets can be used under this BSD license - Last updated on August 30, 2012.
Over 2,400,000 unique IPs last year - Built with WEB4J.
- In Memoriam : Bill Dirani -