7

Possible Duplicate:
What's “@Override” there for in java?

I've never put "@Override" before a method until now. I see some code examples with it, but I don't understand its utility. I'd love some explanation.

Many thanks,

JDelage

Community
  • 1
  • 1
JDelage
  • 11,626
  • 21
  • 74
  • 107
  • Duplicate of [What's "@Override" there for in java?](http://stackoverflow.com/questions/2489974/whats-override-there-for-in-java) – Firas Assaad Nov 15 '10 at 14:45
  • Just to clarify, you say "@Override before a class" - do you mean a method? Or are you actually referring to an @Override annotation on a class? – Rob Hruska Nov 15 '10 at 14:46
  • Also duplicate of http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why – David J. Liszewski Nov 15 '10 at 14:52
  • Sorry - that's indeed a duplicate but I ran a search for @Override and didn't get anything. – JDelage Nov 15 '10 at 15:13
  • hmm, [this search gives some results](http://stackoverflow.com/search?q=[java]+%40Override) (personally I don't care if it's a duplicate, the comment is just to provide a tipp for SO searches) – Andreas Dolk Nov 15 '10 at 15:18

5 Answers5

8

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.

http://download.oracle.com/javase/6/docs/api/java/lang/Override.html

The case I like to explain its use is when overriding equals.

This will error because equals expects an Object parameter:

public class Foo{

    @Override
    public boolean equals(Foo f){
        return true;
    }
}
Jeremy
  • 21,351
  • 4
  • 61
  • 77
  • ...Though I think it can be omitted on methods that really are overrides. I think that will produce a warning in *some* environments, but not an error. – FrustratedWithFormsDesigner Nov 15 '10 at 14:46
  • 1
    It will error if you put `@Override` on a method that doesn't override anything. It definitely is not required, but helps the compiler help you. – Jeremy Nov 15 '10 at 14:49
7

First, you can't annotate a class with @Override. This annotation indicates that a method declaration is intended to override a method declaration in a superclass.

You don't have to annotate overriding methods but if you use this annotation and your annotated method does not override a superclass method, then the compiler will generate an error message.

Andreas Dolk
  • 108,221
  • 16
  • 168
  • 253
5

The best example - overriding equals().

If you write a class like this:

public class Foo 
{
    public String bar;

    public boolean equals(Foo other)
    {
        return this.bar.equals(other.bar);
    }
}

then you've overloaded the equals method, rather than overriding Object.equals as was intended.

If you annotate the equals method with @Override, the compiler will give you an error stating (correctly) that you haven't overridden a superclass method.

In Java 6, you can use this for implementing interface methods too - this is handy when you're only adding a method to your class to satisfy some interface, and hence the compiler can check that it's required and alert you to the interface changing.

As with all annotations it's effectively a programmatic comment, but having the compiler check that your assumptions are (still) correct is very handy in these cases.

Andrzej Doyle
  • 97,637
  • 30
  • 185
  • 225
2

It's there to express that you expect the method to be overriding a superclass method. It does come in handy when you make a mistake spelling the method name or give it the wrong parameters so that it does not override what you thought it was overriding.

Nathan Hughes
  • 85,411
  • 19
  • 161
  • 250
1

It's a conventional comment. Some compilers make sure that the function followed by @Override is actually an override... just a failsafe

Mikhail
  • 8,071
  • 6
  • 45
  • 78