File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Overrides Annotation question?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

Guys,

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: 14150
    
  18

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 7 API documentation
Scala Notes - My blog about Scala
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overrides Annotation question?