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.
satya - Tue May 22 2012 16:50:02 GMT-0400 (Eastern Daylight Time)
Android Java @override
Android Java @override
satya - Tue May 22 2012 16:52:27 GMT-0400 (Eastern Daylight Time)
Here is what java android lang spec says
Here is what java android lang spec says
Annotation type used to mark methods that override a method declaration in a superclass. Compilers produce an error if a method annotated with @Override does not actually override a method in a superclass.
satya - Tue May 22 2012 16:54:00 GMT-0400 (Eastern Daylight Time)
Here is a discussion on stackoverflow
satya - Tue May 22 2012 16:57:00 GMT-0400 (Eastern Daylight Time)
Looks like this is equally applicable for Jdk 1.5 as well
satya - Tue May 22 2012 16:59:46 GMT-0400 (Eastern Daylight Time)
So what does it do?
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.
As stated in that stackoverflow, this becomes more important when you rely on lot of callbacks hosted by a framework such as Android.
So your code will work even if you don't indicate @override but it is a good practice when YOU KNOW FOR sure that you are overriding it.
satya - Tue May 22 2012 17:00:39 GMT-0400 (Eastern Daylight Time)
With or without the @Override you are not changing the run time semantics of Java!!
With or without the @Override you are not changing the run time semantics of Java!!
satya - Tue May 22 2012 17:03:54 GMT-0400 (Eastern Daylight Time)
It will be interesting then to know what other annotations were introduced in Jdk 1.5
It will be interesting then to know what other annotations were introduced in Jdk 1.5
satya - Tue May 22 2012 17:04:14 GMT-0400 (Eastern Daylight Time)
What are JDK 1.5 out of the box annotations?
What are JDK 1.5 out of the box annotations?
satya - Tue May 22 2012 17:09:19 GMT-0400 (Eastern Daylight Time)
a quick summary of 1.5 features, talks about metadata quickly
a quick summary of 1.5 features, talks about metadata quickly
satya - Tue Sep 04 2012 10:44:29 GMT-0400 (Eastern Daylight Time)
Java abstract method and @Override
Java abstract method and @Override
satya - Tue Sep 04 2012 10:48:21 GMT-0400 (Eastern Daylight Time)
Java 6 and @Override for abstract methods
Java 6 and @Override for abstract methods
satya - Tue Sep 04 2012 10:49:19 GMT-0400 (Eastern Daylight Time)
Another useful thing for @Override
If the base class changes and removes a method, on which you are depending, then you get an error saying that method doesn't exist anymore.
satya - Tue Sep 04 2012 10:57:44 GMT-0400 (Eastern Daylight Time)
JDK 1.5 doesn't allow @Override on abstract methods
You can do this from JDK 1.6.
satya - Tue Sep 04 2012 10:59:04 GMT-0400 (Eastern Daylight Time)
Here is quite a comprehensive discussion on this topic from stack overflow
Here is quite a comprehensive discussion on this topic from stack overflow