Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overrides Annotation question?

 
Joe Harry
Ranch Hand
Posts: 10122
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joe Harry
Ranch Hand
Posts: 10122
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right, I got it...thanks for the help!
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic