[java] Should we @Override an interface's method implementation?


I believe that javac behaviour has changed - with 1.5 it prohibited the annotation, with 1.6 it doesn't. The annotation provides an extra compile-time check, so if you're using 1.6 I'd go for it.


Should a method that implements an interface method be annotated with @Override?

The javadoc of the Override annotation says:

Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

I don't think that an interface is technically a superclass. Or is it?

Question Elaboration

For me, often times this is the only reason some code requires Java 6 to compile. Not sure if it's worth it.

Overriding your own methods inherited from your own classes will typically not break on refactorings using an ide. But if you override a method inherited from a library it is recommended to use it. If you dont, you will often get no error on a later library change, but a well hidden bug.

If a concrete class is not overriding an abstract method, using @Override for implementation is an open matter since the compiler will invariably warn you of any unimplemented methods. In these cases, an argument can be made that it detracts from readability -- it is more things to read on your code and, to a lesser degree, it is called @Override and not @Implement.

By reading the javadoc in java8, you can find the following at the declaration of interface Override:

If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:

  • The method does override or implement a method declared in a supertype.
  • The method has a signature that is override-equivalent to that of any public method declared in {@linkplain Object}.

So, at least in java8, you should use @Override on an implementation of an interface method.