I am using eclipse , when I use shortcut to generate override implementations , there is an override annotation up there , I am using JDK 6 , this is all right , but under JDK 5 this annotation will cause an error, so I want to ask , if this annotation is completely useless ? Will compiler do some kind of optimization using this annotation ?
Its purpose is for the compiler to be able to tell you when the method is not in fact overriding the super class method. For example, suppose you misspelled the name, with the anotation the compiler will warn you that the method is not overriding anything, and so you'll be able to catch your error, instead of running your program and not understanding why your method never gets called.
As others have pointed out, the @Override
annotation is really a compiler directive that instructs javac
to scream if a method annotated with @Override
does not actually override a method in its parent class (e.g. you're actually overloading because you've decided to change the method signature or you misspell the method name).
Under JDK 5, directly implementing a method from an interface is not considered overriding that method and is considered an error if annotated with @Override
.
In part due to user feedback that this was a really confusing behaviour, JDK 6 changed this behaviour and does consider it correct to annotate a method you implement from an interface with @Override
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With