Should I put the @Override tag if I am implementing a method of an interface? I know @Override tag should be there when you override a method of super class (not an interface). But how about implementing a method of an interface?
Asked
Active
Viewed 2,248 times
3
-
Possible duplicate - http://stackoverflow.com/questions/212614/should-a-method-that-implements-an-interface-method-be-annotated-with-override/212624#212624 – jjnguy Oct 06 '10 at 01:17
1 Answers
9
Well, yes:
You should use @Override whenever possible. It prevents simple mistakes from being made. Example:
@Override public boolean equals(MyObject mObj){ // code ... }
This doesn't compile because it doesn't properly override equals.
The same will go for methods that implement an interface (1.6 and above only) or override a Super class's method.
-
But how come the netbeans IDE (and now the eclipse Helios) complains when I do something like: new Comparator
(){ @Override public int compare(Obj lhs, Obj rhs){ ..... } } The IDE suggests to remove the @Override tag – dolaameng Oct 06 '10 at 01:20 -
@dolaa, what version of java are you using? 1.5 does not allow annotating interface implementing like this. – jjnguy Oct 06 '10 at 01:22
-
1
-