Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the good of IDE's auto generated @override annotation?

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 ?

like image 957
Sawyer Avatar asked Dec 07 '22 04:12

Sawyer


2 Answers

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.

like image 195
JRL Avatar answered Feb 23 '23 01:02

JRL


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.

like image 34
Ophidian Avatar answered Feb 22 '23 23:02

Ophidian