2

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

I keep running into incompatibility problems due to this annotation. What's the point of having it anyway?

What's the difference between simply overriding a method and overriding a method + annotating it with @override?

2

3 Answers 3

4

To quote from the @Override JavaDoc:

If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

So a good answer would be "to prevent potential run time errors at compile time" :-)

Cheers,

2

The point is to launch an error if you are not really overriding a method.

For example, you have

public class SuperClass {
  public void doSomething(int a) {
  }
}

You may make an error while extending it

public class SubClass {
  public void doSomething(String a) {
  }
}

Now do you believe you have reimplemented the superclass method but you have just added a new one.

Add @Override before the method declaration and a error will be shown, so you know there is something wrong.

1

It make it clear that a method should override a parent's method.

If you are having trouble it suggests you are make many incompatible changes in parent classes (without getting your IDE to do this for you) or you are compiling the code with Java 5.0 which doesn't allow this annotation if you are implementing an interface.

Not the answer you're looking for? Browse other questions tagged or ask your own question.