Java – @Override for in Java?

javaoverriding

Possible Duplicate:
When do you use Java’s @Override annotation and why?

Is there any reason to annotate a method with @Override other than to have the compiler check that the superclass has that method?

Best Solution

As you describe, @Override creates a compile-time check that a method is being overridden. This is very useful to make sure you do not have a silly signature issue when trying to override.

For example, I have seen the following error:

public class Foo {
  private String id;
  public boolean equals(Foo f) { return id.equals(f.id);}
}

This class compiles as written, but adding the @Override tag to the equals method will cause a compilation error as it does not override the equals method on Object. This is a simple error, but it can escape the eye of even a seasoned developer