aspose file tools*
The moose likes Beginning Java and the fly likes question regarding @Override Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question regarding @Override" Watch "question regarding @Override" New topic
Author

question regarding @Override

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

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
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
@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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11295
    
  16

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'.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

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

Joined: Sep 28, 2010
Posts: 151
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

Joined: Aug 08, 2010
Posts: 210

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?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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!



[Jess in Action][AskingGoodQuestions]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14139
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

ok i understand allot more about it now!! thanks for all the answers!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question regarding @Override