patternjavaCritical
Should we @Override an interface's method implementation?
Viewed 0 times
implementationshouldoverridemethodinterface
Problem
Should a method that implements an interface method be annotated with
The javadoc of the
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
@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
Solution
You should use
This doesn't compile because it doesn't properly override
The same will go for methods that implement an interface (1.6 and above only) or override a Super class's method.
@Override whenever possible. It prevents simple mistakes from being made. Example:class C {
@Override
public boolean equals(SomeClass obj){
// code ...
}
}This doesn't compile because it doesn't properly override
public boolean equals(Object obj).The same will go for methods that implement an interface (1.6 and above only) or override a Super class's method.
Code Snippets
class C {
@Override
public boolean equals(SomeClass obj){
// code ...
}
}Context
Stack Overflow Q#212614, score: 350
Revisions (0)
No revisions yet.