4

This is a newbie question. I read that JVM's execution starts from searching for the methodname from lowest class in the hierarchy and if the method is not available in that class it traverses to the parent class looking for the method.

If this is the case then why do we need to use "@override" for adding custom logic to the inherited class ?

The below example illustrates my question

class superclassA
{

method()
{
}

}

class subclassB extends superclassA
{

@Override 
//While executing if JVM starts looking for the method name from the lowest hierarchy
//then why do we have to use "override" as the methodname will be matched from the lowest level itself?
method() 
{
--custom subclass specific code...
} 

}
UnderDog
  • 3,173
  • 10
  • 32
  • 49
  • 3
    You don't need `@Override`. It was introduced only fairly recently (Java 1.5) and is completely optional. - Its purpose is to avoid mistakes in your program because it tells the compiler that you actually *intend* to overwrite a specific method. If, for example, you mistyped the method's name, the inherited method is *not* overwritten and that may cause some nasty bugs. With `@Override` the compiler should at least warn you if the annotated method does *not* actually overwrite an inherited method. – JimmyB Jul 26 '13 at 13:26
  • thanks hanno..it explains – UnderDog Jul 26 '13 at 13:28
  • @HannoBinder 1.5 is fairly recent? It's been out for longer than 1.0 was by the time of the 1.5 release. – Tom Hawtin - tackline Jul 26 '13 at 13:44
  • @TomHawtin-tackline Of course, you're (almost) right. The point actually only is that 1.0, 1.1, 1.2, and 1.4 got along without `@Override`, which implies that enforcing the use of this annotation would syntactically invalidate all programs written for any version below 1.5, leaving 'only' 1.5, 1.6, and 1.7 to be 'compatible'; and that's one obvious reason for `@Override` to be optional and not required. – JimmyB Jul 26 '13 at 14:38

3 Answers3

5

If this is the case then why do we need to use "@override" for adding custom logic to the inherited class?

We don't. The @Override annotation has no technical meaning - it exists to document the fact that this method overrides one in the superclass, which has some advantages:

  • If you look at the code, it tells you there is an superclass method that might be important to understand what this method does
  • You will get a compiler error if the superclass method's signature changes in a way that the subclass method does in fact not override it anymore.
  • You can get a compiler warning if you override a method without using the annotation, in case you do it inadvertantly.
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
2

@Override simply helps Java compiler detect errors in source code, compilers are generate an error if a method annotated with @Override does not override it in fact.

It is not mandatory to annotate a method that overrides a supertype methods with @Override.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

You don't need @Override. But it's a useful annotation that causes the compiler to check whether or not you are really overriding the method that you say you are. When you @Override a method that is not actually overriding a method, the compiler will inform you of this discrepancy. Additionally, it just makes your code more clear: since all methods in Java are implicitly virtual, and a method in a derived class with the same signature as that of a non-final method in a super class implicitly overrides it1, adding @Override makes the code easier for humans to understand.

1: To be clear, you can not have a method in a derived class with the same signature as a final method in a super class.

jason
  • 236,483
  • 35
  • 423
  • 525