tags:

views:

236

answers:

6
public class Animal {
   public void eat() { System.out.println("I eat like a generic Animal."); }

}

public class Wolf extends Animal {
   @Override
   public void eat() { System.out.println("I eat like a wolf!"); }
}

Does @Override actually have some functionality or it's just kinda comment?

+11  A: 

Override annotation is a compile time annotation which makes java compiler throw an error if the method having this annotation is actually not overriding a parent class method. You can try to change the Wolf class to not extend the Animal class and if Override annotation is present it will show you a compile time error

Fazal
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Override.html
Woot4Moo
Will there be any difference in functionality if there is no `@Override ` in my code above?
symfony
@symfony No not in the above code. But if you mispelled eat as eats and did not have the @override, then the compiler would think you are creating a new method and would compile fine with no warning. It allows the compiler to find mistakes. More commonly it helps the developer find mistakes were the used the wrong type in a method parameter, such as doing `public boolean equals(MyObject o)` instead of `public boolean equals(Object o)`.
Brandon Bodnár
Last,is it case sensitive?I see you write @override instead of @Override
symfony
Yeah it is case sensitive. That was just a typo in my comment. Still waiting for SO to add compiler checking to the comment sections. :)
Brandon Bodnár
+1  A: 

something like it alerts at compile time by throwing compilation error if you are not really overriding the method. similar Q here-http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why

daedlus
A: 

It works as a comment, but also as an assertion that you actually ARE overriding something. If you use @Override, but it does not actually override anything, the compiler will generate an error. See the documentation page for more details.

Spodi
A: 

If you remove the "eat" method from the parent class or misspell it as "eats" in the subclass, your code will now fail to compile. Without the "@Override" annotation, you can do either and your code will still compile, it just won't do what you want (namely, override a method).

Ross
+7  A: 

From the Java Tutorials on annotations:

@Override — the @Override annotation informs the compiler that the element is meant to override an element declared in a superclass (overriding methods will be discussed in the the lesson titled "Interfaces and Inheritance").

   // mark method as a superclass method
   // that has been overridden
   @Override 
   int overriddenMethod() { }

While it's not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

Let's take a look at the example given in the Java Language specifications, 9.6.1.4 Override. Let's say you want to override a method, equals in that case, but you wrote:

    public boolean equals(Foo that) { ... }

instead of:

    public boolean equals(Object that) { ... }

While this code is legal, annotating the equals method declaration with @Override would trigger a compile time error because you're in fact not overriding it, you're overloading it. This can cause nasty bugs and the Override annotation type helps at detecting them early.

Pascal Thivent
So it doesn't affect the run time behavior,right?
symfony
Correct. It will not affect anything after build time.
Marc W
However, it will prevent builds that are in error.
Thilo
Is annotation case sensitive?
symfony
@symfony Yes it is, an annotation is a type.
Pascal Thivent
A: 

It does not do anything at run-time, but it helps you to catch errors:

If you thought you would override a method, but do not (because of a speling problem or parameter types), without the annotation, the code would compile to something useless (as in: your method would not be called, but the superclass method that you accidentally did not override gets called).

Thilo