3

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?

dolaameng
  • 1,397
  • 2
  • 17
  • 24
  • 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 Answers1

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.

Community
  • 1
  • 1
jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • 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
    But, you should definitely do it if you are using 1.6 – jjnguy Oct 06 '10 at 01:23
  • @dola, well, it that case you cannot use the @Override annotation. – jjnguy Oct 06 '10 at 01:24