9-May-14 (Created: 9-May-14) | More in 'CS-Java'

Whats up with Java Override?

For some time now Java compilers allow you indicate some methods in your class definition as overridden methods.

You do this by annotating the method signature with a code snippet such as


@Override
public void somemethod(){}

By doing so you tell the compiler to see if this annotated method exists in base classes or in the inheritance hierarchy. If it is, nothing happens. If it isn't you, the programmer, have made a mistake, because you expected it to be there. So perhaps misspelled it.

In JDK 1.5 this annotation will throw an error if the method is abstract in the base class.

Either it is corrected or deemed more useful in later JDKs and you can now safely provide this annotation on methods whose baseclass representations are abstract.

this annotation will also guard against some of the base class changes where a method is removed which is overridden in derived classes. You now will know that that happened.

this protection is especially important in frameworks where lot of callbacks are used like the container managed Android components.

bottom line, say @override if you expect this method to be in the base class and you are overriding it.