开发者

@Override Snafu

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 far 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 googling, I got to know there is something like OVER开发者_如何学PythonRIDE_SNAUF - where 6.0 Java compiler was updated to allow @Override on interface method implementations.


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜