What are the best practices for using Java's @Override
annotation and why?
It seems like it would be overkill to mark every single overridden method with the @Override
annotation. Are there certain programming situations that call for using the @Override
and others that should never use the @Override
?
Use it every time you override a method for two benefits. Do it so that you can take advantage of the compiler checking to make sure you actually are overriding a method when you think you are. This way, if you make a common mistake of misspelling a method name or not correctly matching the parameters, you will be warned that you method does not actually override as you think it does. Secondly, it makes your code easier to understand because it is more obvious when methods are overwritten.
Additionally, in Java 1.6 you can use it to mark when a method implements an interface for the same benefits. I think it would be better to have a separate annotation (like @Implements
), but it's better than nothing.