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?
+3
A:
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.
jjnguy
2010-10-06 01:18:38
But how come the netbeans IDE (and now the eclipse Helios) complains when I do something like:new Comparator<Obj>(){ @Override public int compare(Obj lhs, Obj rhs){ ..... }}The IDE suggests to remove the @Override tag
dolaameng
2010-10-06 01:20:36
@dolaa, what version of java are you using? 1.5 does not allow annotating interface implementing like this.
jjnguy
2010-10-06 01:22:42
But, you should definitely do it if you are using 1.6
jjnguy
2010-10-06 01:23:04
Yes, I am using java 1.5. Got it thanks!
dolaameng
2010-10-06 01:24:23
@dola, well, it that case you cannot use the @Override annotation.
jjnguy
2010-10-06 01:24:58