• 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

@Override

 
Ranch Hand
Posts: 65
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Often I see the word @Override ahead of function/method definitions.
In my books about Java I don't find this mentioned.

Is it necessary if one wants to overwrite a method ?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not required. It's a hint to the compiler that the method is intended to override some other method. That allows the compiler to check that it actually does override some method. Otherwise, a typo in the method name or incorrect parameter types would lead to no method being overridden - and you'd be left wondering why your method doesn't get called. Also see http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html

If your books don't cover it that may be a sign that they're somewhat old - it was introduced only with Java 5.
 
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
To give you a concrete example: Consider the hashCode() method of class Object. Sometimes you want to override that method in your own classes. But it's easy to forget that the method is called hashCode(), and not hashcode() with a lower-case c. When you do this:

You will get a compiler error, because the compiler notices that your hashcode() method is not overriding any method from a superclass - so it helps you to catch your error.
 
Wolfgang Tintemann
Ranch Hand
Posts: 65
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your clear and exhaustive answers.
 
reply
    Bookmark Topic Watch Topic
  • New Topic