views:

73

answers:

1

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
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
@dolaa, what version of java are you using? 1.5 does not allow annotating interface implementing like this.
jjnguy
But, you should definitely do it if you are using 1.6
jjnguy
Yes, I am using java 1.5. Got it thanks!
dolaameng
@dola, well, it that case you cannot use the @Override annotation.
jjnguy