Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - what is "@Override" used for? [duplicate]

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

like image 922
JDelage Avatar asked Nov 15 '10 14:11

JDelage


1 Answers

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;
    }
}
like image 53
Jeremy Avatar answered Sep 20 '22 18:09

Jeremy