Should I add an @Override annotation when implementing abstract methods in Java?

Eyvind picture Eyvind · Jun 17, 2009 · Viewed 17.9k times · Source

When overriding a non-virtual method in Java, use of the @Override annotation is recommended, but what if I implement an abstract method? Should I use @Override then as well?

Answer

Andrzej Doyle picture Andrzej Doyle · Jun 17, 2009

I tend to prefer the use of @Override in this case, so that the method gets flagged in the subclasses if the superclass changes (either removing the method altogether, or changing its signature, etc.).

The only real difference is that without the annotation, if the method in the superclass/interface is changed or removed, the implementation in question simply becomes a "normal" method of that class. Thus you should add the annotation if you're implementing the method solely to fulfil the contract; and you probably shouldn't add it if the method makes sense in your class regardless of any implemented interfaces or inherited abstract methods.