Should we @Override an interface's method implementation?
Asked Answered
S

15

491

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

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?

Question Elaboration

Suburban answered 17/10, 2008 at 15:18 Comment(3)
I can't find the replacement for the @Override article (Oracle moved the old Sun blogs recently). Do you know how to find it?Rambert
We should have an @Implement(s) annotation by now (2015). That will make things clear!Heraclitean
by now (2015) should we use @Override with java 8?Rossi
C
343

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

class C {
    @Override
    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.

Crites answered 17/10, 2008 at 15:21 Comment(12)
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.Puss
Um, no, it doesn't. In fact, Eclipse auto-inserts @Override when filling in methods that implement an interface.Crites
-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.Minnie
Fixed...I guess I figure everyone uses the latest version of Java. My bad.Crites
+1 for the after 1.5 bit. Took me like 20 minutes to figure out why eclipse was giving me errors and with this I realized my new project was using java 1.5Purge
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.Differentiate
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.Hendershot
Eclipse does not allow the @Override notation for methods that are not inherited by a superclass.Missile
this rule makes no sense to me. unless im wrong, interfaces tell us what functions are needed, not what's fully defined. essentially, there's nothing to override cuz it hasn't happened yet. i would personally only want to put it for superclass methods. not only that, all public methods now have @Override if you follow java's testing practice recommendations for creating an interface all the timeEncephaloma
This is not an answer to the question. Object is not an interface.Sluice
Although encouraged, annotations are completely optional. It's metadata!Convalescence
@Convalescence If you put an "Override" annotation on something that is not overriden, it will not build. That's what this answer is saying. This way, you are making sure that you are really overriding a method, and not creating a new one if you mispelled the method name or something.Entranceway
S
118

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.

Scintillant answered 17/10, 2008 at 15:28 Comment(0)
N
83

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 compiler 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 preferred to see a different annotation for this usage, maybe an @Implements annotation.

Nitrochloroform answered 25/11, 2011 at 9:10 Comment(0)
P
17

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

Plutonium answered 17/10, 2008 at 15:27 Comment(0)
D
12

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

Dolora answered 8/6, 2009 at 12:1 Comment(0)
P
7

By reading the javadoc in java8, you can find the following at the declaration of interface Override:

If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:

  • The method does override or implement a method declared in a supertype.
  • The method has a signature that is override-equivalent to that of any public method declared in {@linkplain Object}.

So, at least in java8, you should use @Override on an implementation of an interface method.

Phelips answered 9/3, 2018 at 2:26 Comment(0)
K
6

If a concrete class is not overriding an abstract method, using @Override for implementation is an open matter since the compiler will invariably warn you of any unimplemented methods. In these cases, an argument can be made that it detracts from readability -- it is more things to read on your code and, to a lesser degree, it is called @Override and not @Implement.

Kelle answered 19/12, 2015 at 0:48 Comment(0)
D
3

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.

Darned answered 17/10, 2008 at 21:16 Comment(0)
S
3

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.

Scatology answered 3/12, 2010 at 5:50 Comment(0)
G
3

If the class that is implementing the interface is an abstract class, @Override is useful to ensure that the implementation is for an interface method; without the @Override an abstract class would just compile fine even if the implementation method signature does not match the method declared in the interface; the mismatched interface method would remain as unimplemented. The Java doc cited by @Zhao

The method does override or implement a method declared in a supertype

is clearly referring to an abstract super class; an interface can not be called the supertype. So, @Override is redundant and not sensible for interface method implementations in concrete classes.

Garish answered 2/2, 2019 at 7:0 Comment(1)
It is more than redundant. In STS4, when a class method implementing interface method was decorated with @Override, compiler error message says "...must override a superclass method".Josiahjosias
A
2

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

Abohm answered 29/1, 2009 at 0:52 Comment(0)
S
2

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...

Shuster answered 19/3, 2014 at 20:47 Comment(1)
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.Sincere
G
2

In java 6 and later versions, you can use @Override for a method implementing an interface.

But, I donot think it make sense: override means you hava a method in the super class, and you are implementing it in the sub class.

If you are implementing an interface, I think we should use @Implement or something else, but not the @Override.

Goethe answered 28/9, 2016 at 6:57 Comment(1)
I agree, no other answer really justify why @override should used for an interface implementation. The only reason I'd say it could be useful is if you'd change a class to an abstract class, that reasonably speaking, should not happen.Spoilage
A
2

This might be too late answer. But I hope this example would help someone to understand why @override is so important (for a scenario like this)

public interface Restaurant(){
    public boolean getBill();
    public BigDecimal payAmount();
}

public interface Food() extends Restaurant{
    public boolean haveSomeFood();
    public boolean drinkWater();
}

public class Hungry() implements Food{
    public boolean haveSomeFood(){}
    public boolean drinkWater(){}
    public boolean getBill(){}
    public BigDecimal payAmount(){}
}

In the above example, If I am Hungry, I can have Food from a Restaurant. But if I took out the implementation of Restaurant, I do not have to get a bill and no need to pay the amount!!!!

How?

  • The interface Food has the keyword extends Restaurant - which means the class Hungry is also implemented from the interface Restaurant.
  • The methods actually overriden from the interface does not have the keyword @override
  • So, if I remove the keyword extends Restaurant (or say if I remove the interface Restaurant, it will not show any error.

If there was @override, whenever I am trying to remove the Restaurant interface it will show compilation error.

Note: When you implement an interface, you might not know whether that interface is extended to another interface or can be extended or the inheritance may be removed. So it is always better to use @override keyword

Ambrosia answered 25/1, 2022 at 20:51 Comment(2)
Isn't it a very bad design to use inheritance to model the relationship between food and restaurant at first place? There is another way called composition. We shall never rely on the mighty power of a language to overcome the challenge brought by impotent designInnocency
@RuiZheng - Doesn't matter the design as the OP asked about override - Should we @Override an interface's method implementation?. I just wanted to describe why @Override is important. Of course, you are right, food can have from a restaurant or from home or from anywhere!. In my example, the tight coupling between food & restaurant is wrong. Thank you for noticing it (but I already thought about this scenario, didn't want to confuse describing those here)Ambrosia
R
1

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

Redivivus answered 17/10, 2008 at 21:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.