views:

6136

answers:

8

Intro

My real question is about the use of the annotation. Trying to find an answer myself, I ran into several other questions. This is why there are also related questions below. I hope this is not too confusing.

Question

Should a method that implements an interface method be annotated with @Override? Eclipse for instance automatically inserts an @Override annotation after using the Quick Fix option 'Add unimplemented methods'. Is this correct behavior?

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? Compilers don't generate an error at least when the annotation is used in this case.

Eclipse can give warnings if you omit this annotation for overriding ''real'' superclass methods. It doesn't if you omit it for interface methods. Another question revealed that this is a glitch in Eclipse.

The reason this worries me a little bit, is that I try to be consistent. But if the IDE doesn't give warnings when the annotation is not there and automatically generates them on the other hand, consistency is rather difficult.

Edit: Two articles with relevant info: @Override and @Override Snafu

+19  A: 

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
Note that you **cannot** add the @Override annotation to a method implementing an interface in Java 5 - it generates an error. It is allowed in Java 6.
Bill Michell
How can people vote up an incorrect answer? You are NOT allowed to @Override an interface method... this generates a compiler error.
Matthias
Um, no, it doesn't. In fact, Eclipse auto-inserts @Override when filling in methods that implement an interface.
jjnguy
-1 until the answer includes a mention about the different behaviour from Java 1.5 to 1.6 with regards to implementing an interface method. Just because I've seen it be a confusing aspect for people and it really merits a mention.
Grundlefleck
Fixed...I guess I figure everyone uses the latest version of Java. My bad.
jjnguy
+1 for the added reference :)
Grundlefleck
+1 for someone finally beating Jon Skeet
BlueRaja - Danny Pflughoeft
Ha, thanks BlueRaja.
jjnguy
@BlueRaja: it's not absolutely unheard of ;-) (http://stackoverflow.com/questions/1981769/method-chaining-with-value-objects/1981827#1981827)
Grundlefleck
+5  A: 

I would use it at every opportunity. See When do you use Java's @Override annotation and why?

Alex B
+16  A: 

I believe that javac behaviour has changed - with 1.5 it prohibited the annotation, with 1.6 it doesn't. The annotation provides an extra compile-time check, so if you're using 1.6 I'd go for it.

Jon Skeet
+1  A: 

Overriding your own methods inherited from your own classes will typically not break on refactorings using an ide. But if you override a method inherited from a library it is recommended to use it. If you dont, you will often get no error on a later library change, but a well hidden bug.

Arne Burmeister
A: 

Eclipse itself will add the @Override annotation when you tell it to "generate unimplemented methods" during creation of a class that implements an interface.

savetheclocktower
+2  A: 

For me, often times this is the only reason some code requires Java 6 to compile. Not sure if it's worth it.

Fabian Steeg
It's worth it...
Thilo
+1  A: 

JDK 5.0 not allows you to use @Override annotation if you are implementing method declared in interface (its compilation error), but JDK 6.0 allows same. So may be you can configure your project preference according to your requirement.

Silent Warrior
A: 

How can I change the eClipse / QuickFix not to put the @Override in?

Tim