Suppose you have:
public class Foo
{
public void bar(String x, String y) {}
}
public class Foo2 extends Foo
{
public void bar(String x, Object y) {}
}
You really meant Foo2.bar
to override Foo.bar
, but due to a mistake in the signature, it doesn't. If you use @Override
you can get the compiler to detect the fault. It also indicates to any reading the code that this is overriding an existing method or implementing an interface - advising them about current behaviour and the possible impact of renaming the method.
Additionally, your compiler may give you a warning if a method overrides a method without specifying @Override
, which means you can detect if someone has added a method with the same signature to a superclass without you being aware of it - you may not want to be overriding the new method, as your existing method may have different semantics. While @Override
doesn't provide a way of "un-overriding" the method, it at least highlights the potential problem.