File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Overrides Annotation question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overrides Annotation question?" Watch "Overrides Annotation question?" New topic

Overrides Annotation question?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10031


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!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10031

All right, I got it...thanks for the help!
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

Actually, some IDE's like Eclipse warn you when @Override is missing. Great feature too - if I don't get a warning I also know I haven't overridden it.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Overrides Annotation question?
It's not a secret anymore!