• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overrides Annotation question?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All right, I got it...thanks for the help!
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic