Take the 2-minute tour ×

Should a method that implements an interface method be annotated with @Override?

The javadoc of the Override annotation says:


I don't think that an interface is technically a superclass. Or is it?

Question Elaboration

share|improve this question
wow this question could be shorter, but it's the question I needed. Thanks –  Yar Feb 25 '10 at 2:39
I can't find the replacement for the @Override article (Oracle moved the old Sun blogs recently). Do you know how to find it? –  Bill the Lizard Jul 26 '12 at 15:24

10 Answers 10

up vote 136 down vote accepted

You should use @Override whenever possible. It prevents simple mistakes from being made. Example:

class C {
    public boolean equals(SomeClass obj){
        // code ...

This doesn't compile because it doesn't properly override public boolean equals(Object obj).

The same will go for methods that implement an interface (1.6 and above only) or override a Super class's method.

share|improve this answer
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 Oct 22 '08 at 9:39
Um, no, it doesn't. In fact, Eclipse auto-inserts @Override when filling in methods that implement an interface. –  jjnguy Jan 15 '10 at 17:26
-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 Jan 15 '10 at 17:32
If eclipse is complaining then upgrade ur jdk to > 1.5 and change the compiler compliance level to 1.6 or 1.7. To do that right click on ur project-> properties-> Java compiler and select one that is higher than 1.5. –  Rose Nov 14 '12 at 19:18
Can anyone think of an example that actually justifies the answer (implementing interfaces rather than overriding base methods)? A big plus for me is that it aids sets a readers expectations of how and by what a particular method might be used. –  yjo May 27 at 17:13

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.

share|improve this answer
What is the extra check? –  Michael Carman Feb 16 '12 at 16:38
@Michael You can notice if any interface has deleted. –  Sangdol Feb 27 '12 at 0:26
@Sangdol +1 It is the only concrete and valid answer to the original question. –  Fernando Pelliccioni Oct 21 at 18:27

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

share|improve this answer

You should always annotate methods with @Override if it's available.

In JDK 5 this means overriding methods of superclasses, in JDK 6, and 7 it means overriding methods of superclasses, and implementing methods of interfaces. The reason, as mentioned previously, is it allows the compilor to catch errors where you think you are overriding (or implementing) a method, but are actually defining a new method (different signature).

The equals(Object) vs. equals(YourObject) example is a standard case in point, but the same argument can be made for interface implementations.

I'd imagine the reason it's not mandatory to annotate implementing methods of interfaces is that JDK 5 flagged this as a compile error. If JDK 6 made this annotation mandatory, it would break backwards compatibility.

I am not an Eclipse user, but in other IDEs (IntelliJ), the @Override annotation is only added when implementing interface methods if the project is set as a JDK 6+ project. I would imagine that Eclipse is similar.

However, I would have prefer to see a different annotation for this usage, maybe an @Implements annotation.

share|improve this answer
+1, this answer is the only one that explains the situation. –  Pacerier Nov 5 at 23:59

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.

share|improve this answer

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

share|improve this answer
It's worth it... –  Thilo Dec 18 '09 at 11:04

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.

share|improve this answer

It's not a problem with JDK. In Eclipse Helios, it allows the @Override annotation for implemented interface methods, whichever JDK 5 or 6. As for Eclipse Galileo, the @Override annotation is not allowed, whichever JDK 5 or 6.

share|improve this answer

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

share|improve this answer

The problem with including the @Override is that it makes you think that you forgot to call the "super.theOverridenMethod()" method, which is VERY CONFUSING. This should be crystal-clear. Perhaps Java should offer an "@Interface" to be used here. Oh well, yet another half-assed Java peculiarity...

share|improve this answer
Calling a super, when not implementing an interface, is not something you always need to or want to do. Sometimes, you're adding functionality -- so you call it. Other times, you're replacing functionality, so you don't call it. An API author should document whether it relies on internal functionality or not and create a documented contract on how the class can be properly extended. –  lilbyrdie Jun 3 at 15:19

Your Answer


