• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

@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: 43028
76
  • 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.
 
Die Fledermaus does not fear such a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic