Should we @Override an interface's method implementation?

Viewed 206995

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

15 Answers

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.

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.

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.

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.

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.

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

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.

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

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

Related