java can methods - Should we @Override an interface's method implementation?
Should a method that implements an interface method be annotated with
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?
I would use it at every opportunity. See When do you use Java's @Override annotation and why?
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.
For me, often times this is the only reason some code requires Java 6 to compile. Not sure if it's worth it.
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
In java 6 and later versions, you can use
@Override for a method implementing an interface.
But, I donot think it make sense: override means you hava a method in the super class, and you are implementing it in the sub class.
If you are implementing an interface, I think we should use
@Implement or something else, but not the
For the interface, using @Override caused compile error. So, I had to remove it.
Error message went "
The method getAllProducts() of type InMemoryProductRepository must override a superclass method".
It also read "
One quick fix available: Remove @Override annotation."
It was on Eclipse 4.6.3, JDK 1.8.0_144.