Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug with Override annotations in Eclipse

I have a annoying problem with @Override annotations in Eclipse. Often when i import working projects on a new PC, Eclipse marks some of the @Override annotations as wrong. If i remove the annotations everything is fine and Eclipse also indicates that the methods are overriding the parents methods but adding the Override annotation causes the error again. I am currently working on an Android project so it might be a problem with Android and not with Eclipse..

like image 337
Simon Avatar asked Sep 17 '10 13:09

Simon


People also ask

Is @override annotation mandatory?

@Override @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will be discussed in Interfaces and Inheritance. While it is not required to use this annotation when overriding a method, it helps to prevent errors.

What is the override annotation?

The @Override annotation indicates that the child class method is over-writing its base class method. The @Override annotation can be useful for two reasons. It extracts a warning from the compiler if the annotated method doesn't actually override anything. It can improve the readability of the source code.

Can Class override annotation be applied?

The @Override annotation denotes that the child class method overrides the base class method. For two reasons, the @Override annotation is useful. If the annotated method does not actually override anything, the compiler issues a warning. It can help to make the source code more readable.


3 Answers

This is most likely because you are switching between Java 1.5 and Java 1.6. In 1.5 you couldn't mark interface implementations with @Override, but you can in 1.6.

A quick Google search turned up this good explanation of the difference in this annotation between the two versions: http://www.techienuggets.com/CommentDetail?tx=38155

Semantics of @Override is different in JDK 1.5 and JDK 1.6. In JDK 1.5, the @Override annotation is not allowed for implementations of methods declared in an interface, while they are allowed in JDK 1.6. For more information, see:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5008260 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6399361 http://blogs.oracle.com/ahe/?entry=override

It is fact that the description of the Override annotation was not updated in the JDK API docs. This has been reported as a bug:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6501053bugdatabase/view_bug.do?bug_id=6501053

like image 179
Mark B Avatar answered Sep 21 '22 15:09

Mark B


just go to

 window -> prefrences -> java -> compiler 

and set it to 1.6 as notation starts from 1.6 so if compilence level will less then 1.6 it will gives error

like image 30
neeraj t Avatar answered Sep 21 '22 15:09

neeraj t


There are a few places where Java Compiler settings are configured.

One way is to Window->Preferences->Java->Compiler->Compiler Compliance Level -> set 1.6 or above. Another way is right click on the Project->Properties->Java Compiler->JDK Compliance -> Select 1.6 or above.

Also You can unselect "Enable Project Specific Settings" , this will eliminate future JDK compiler compliance errors. Remove Multiple instances of JDK Versions , unless different projects need them.

like image 38
kanaparthikiran Avatar answered Sep 17 '22 15:09

kanaparthikiran