Why does Eclipse complain about @Override on interface methods?
Using the @Override
annotation on methods that implement those declared by an interface is only valid from Java 6 onward. It's an error in Java 5.
Make sure that your IDE projects are setup to use a Java 6 JRE, and that the "source compatibility" is set to 1.6 or greater:
- Open the Window > Preferences dialog
- Browse to Java > Compiler.
- There, set the "Compiler compliance level" to 1.6.
Remember that Eclipse can override these global settings for a specific project, so check those too.
Update:
The error under Java 5 isn't just with Eclipse; using javac
directly from the command line will give you the same error. It is not valid Java 5 source code.
However, you can specify the -target 1.5
option to JDK 6's javac
, which will produce a Java 5 version class file from the Java 6 source code.
@Override compile error, implementing an interface (eclipse jre7 Windows)
I think have solved it. Earlier I was only modifying the workspace properties only but using maven project with SVN. Maven was internally setting property of each project to use "Project Specific 1.5 java compliance" at each update.
I changed each project to use workspace JDK compliance and removed project specific settings in SVN projects to prevent from future errors after code update.
Thanks.
Why is javac failing on @Override annotation
The @Override
annotation spec changed in Java 1.6. In Java 1.5, the compiler did not allow the @Override
annotation on implemented interface methods, but in 1.6 it does. First search result I found is a blog post here.. It was not well documented, but it did change.
Eclipse is adding it because your Eclipse is set for 1.6 compliance. You should try to keep your build and eclipse environments on the same version of Java. It's unclear to me by your specifying Cruise Control is running Java 5 on whether or not it is compiling using a separate JDK6 or not.
Separate from the above 1.5 vs 1.6 @Override
annotation rules, remember that Eclipse has its own compiler implementation (not javac
) and will occasionally have different behavior. Whenever something compiles in Eclipse, but not Ant or Maven, you will need to find a way to make both compilers happy.
Error on @Override annotation with interface implementation
Realized no Builder is selected for current project. Selected Java Builder
and boom. No more red flags all over the code.
Add warning for missing @Override annotation for interface implementation in Eclipse
It is not possible in Eclipse 3.4.1. It is a known issue. See this bug report page for more information.
Should we @Override an interface's method implementation?
You should use @Override whenever possible. It prevents simple mistakes from being made. Example:
class C {
@Override
public boolean equals(SomeClass obj){
// code ...
}
}
This doesn't compile because it doesn't properly override public boolean equals(Object obj)
.
The same will go for methods that implement an interface (1.6 and above only) or override a Super class's method.
Won't let me override interface method
You are using jdk1.5, switch over to jdk 1.6.
In 1.5 you can't use @override for interface methods while in 1.6 you can. This is the issue you are facing.
@Override on interface methods Eclipse
Make sure there are no project-specific settings that are overriding your defaults. Sometimes, particularly when importing projects, Eclipse will assign a project-specific setting that is less than 1.6.
Related Topics
Java Wait for Thread to Finish
Bidirectional Multi-Valued Map in Java
Managing Constructors with Many Parameters in Java
How to Call a Method with a Separate Thread in Java
How to Make a Java Class That Implements One Interface with Two Generic Types
How to Find All the Methods That Call a Given Method in Java
Why Can't a Generic Type Parameter Have a Lower Bound in Java
Load Resource from Anywhere in Classpath
Selenium Ie Webdriver Only Works While Debugging
How to Set the Jdk Netbeans Runs On
Using Jasperreports with a Relative Path
Why Does Sun.Misc.Unsafe Exist, and How Can It Be Used in the Real World
The Simplest Way to Comma-Delimit a List
Java Linkedhashmap Get First or Last Entry
Bigdecimal Equals() Versus Compareto()
Spring Autowiring Using @Configurable
What Does It Mean: the Serializable Class Does Not Declare a Static Final Serialversionuid Field