Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.5k points)

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?

1 Answer

0 votes
by (46k points)

You can try @Override whenever required. It restricts simple mistakes from happening . 

Example:

class C {

    @Override

    public boolean equals(SomeClass obj){

        // code ...

    }

}

This doesn't execute because it doesn't correctly override public boolean equals(Object obj).

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

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...