I was surfing some tutorials on Annotations and came across the Override built in annotation. Actually the purpose of it is to catch the errors related to overridden methods in subclass. Now what is the exact use of this built in feature?? My IDE very well catches this even before I compile my classes from it. Any help??
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
You use the @Override annotation on a method to make clear that the method is supposed to override a method in a superclass. If in reality the method isn't overriding a method in a superclass, the compiler will give you an error message.
Your IDE is not checking for this automatically if you don't have the @Override annotation on your method, because your IDE isn't psychic - it cannot know if you intend a method to be overriding a method in a superclass.
Here's an example. One mistake that people sometimes make when they implement the hashCode() method is that they spell it wrong - they call it hashcode() with a lower-case c. If you call your method hashcode(), then you're not overriding hashCode() in Object, as intended - you're simply adding a new method with a different name. If you would have added @Override to your hashcode() method, the compiler would have caught the mistake, because the compiler then checks if hashcode() is overriding a method in the superclass.
Compiling this will give an error message:
[ October 10, 2007: Message edited by: Jesper Young ]