views:

78

answers:

3

i've create a project/class file in Eclipse Helios using JDK1.6. I had let the eclipse to generate the code for the implementation class of an Interface.

public interface Foo {
    void bar();
}

public class FooImpl implements Foo {
    @Override
    public void bar() {
    }
}

So for so good. Now for some reason, I've imported the project in an Eclipse which has JDK 1.5 and I see error message The method bar() of type FooImpl must override a superclass method with a quick fix to remove '@Override' annotation.

After goodling, I got to know there is something like OVERRIDE_SNAUF - where 6.0 Java compiler was updated to allow @Override on interface method implementations.

+1  A: 

@Override is good for checking override syntax in compile stage, so it is also applied to interface for same reason I guess.

卢声远 Shengyuan Lu
It also refers the javadoc of the method definition in the interface, so javadoc doesn't need to be copy-pasted over. Yay for DRY!
oksayt
+1  A: 

Can't find a question but - yes, you're right, the @Override annotation was not allowed to annotate overriden interface methods in Java 5. So you'll have to remove those annotations if you want to compile the code with Java 5.

Andreas_D
A: 

You can set your compiler options to compliance level 1.5 even if you have jdk 1.6. That way the code generated by eclipse will be compatibile with java 1.5 (it won't add the @Override annotation on methods that implement an interface). I think you can in fact set it to never put an @Override annotation at all.

Well, actually it's a tad more complicated than that. For full compatibility you should install both JDK 1.6 and JDK 1.5 and set each one as the default JDK for their compliance level. That's the best way to ensure full backwards compatibility of your code.

In the preferences window (general, or for a specific project) go to Java->Compiler and set the compliance level there. Also in Java->Installed JREs add JDK 1.5, and then in Java->Installed JREs->Execution Environments click on J2SE-1.5 and check the newly installed JDK 1.5. This ensures that eclipse will use the compiler behaviour as well as the standard library that comes with JDK 1.5 if your project or workspace is set to use that compliance level.

Andrei Fierbinteanu