• 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

question regarding @Override

 
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I just started finally learning OOP in my Java course.One of the first things has been about Overriding, when i use @Override before a method in a subclass,i am actually saying add from the Super class to the sub class correct? In my book it is saying overwrite , but i don't think that is a correct term of what i see happening.

any insight would be of great help

Thanks
Mike
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Override doesn't actually do anything. It just tells the compiler to spit out an error if that method isn't overriding a method from a super class. It doesn't actually change how your code works.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm not sure, but I think the @Override is nothing more than an annotation - it is not ever required.

However, if you use it, the compiler knows that you are trying to override a method in the parent class, and will let you know if you have a wrong set of parameters or even simply misspell the name. If the parent class has a 'doStuff' method, and in your child class you create a 'dostuff' method, without the @Override your code compiles but you have two separate methods which may be hard to find. However, if you had the @Override, it would complain at compile time that something was amiss...

It doesn't cause the method to be pulled down from the parent - you get that for free from the 'extends Parent'.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for your quick answers, in the first set of samples the toString() method is annotated with @Override in the parent and child class, and the output from both classes was then written all together.
I think that the toString() is a special case maybe?
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

Compile and run it with the annotation. Then compile and run it without the annotation. Nothing will change.

Now put the annotation back in, but change the name of the toString method to something else: toStringSpelledWrong(). Compile. What happens?
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at kevin,

ok it worked properly without the annotation,but then...why do i bother with it? and with it but one of the methods spelled differently i got "The method toforkString() of type ElternBear must override or implement a supertype method" So there must be two same named methods in the parent and child class correct?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mike ryan wrote:why do i bother with it?



You bother with it because it helps catch mistakes. If you think you're supposed to be overriding a method, but for some reason you're not, then the compiler will complain. Getting the compiler to complain when you make a mistake is a big part of designing a robust programming environment!


 
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
If you intend a method to be overriding a method from a superclass, it is always a good idea to use the @Override annotation - this will protect you from unintentionally not overriding a method from the superclass.

A typical example is the hashCode() method. There's a hashCode() method in class Object, and in many situations you want to override it. It's easy to forget that it's called hashCode() with a capital C; you might accidentally write hashcode() with a lower-case c. If you're not using the @Override annotation, then at compile time the compiler will not notice that you made a mistake, and your program might appear to work correctly - but it does contain a bug. Had you used the annotation, like this:

then the compiler would have reported an error, because hashcode() is not actually overriding hashCode() from class Object as you intended.

So, it's meant to prevent you from making mistakes.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i understand allot more about it now!! thanks for all the answers!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic