1

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 OVERRIDE_SNAUF - where 6.0 Java compiler was updated to allow @Override on interface method implementations.

tk_
  • 16,415
  • 8
  • 80
  • 90
jai
  • 21,519
  • 31
  • 89
  • 120
  • I'm not an expert on Java but you're not overriding the methods since the interface doesn't provide an implementation, but I guess you figured that out yourself. What's the question then? Just thinking out loud? – Novikov Sep 28 '10 at 05:33
  • 2
    What is the question? How to get consistent behavior between Java 6 and Java 5. Maybe check http://stackoverflow.com/questions/2135975/can-i-get-java-5-to-ignore-override-errors. But why are you using Java 5 by the way? – Pascal Thivent Sep 28 '10 at 05:54

3 Answers3

1

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

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
  • 3
    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 Sep 28 '10 at 05:40
1

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 Dolk
  • 113,398
  • 19
  • 180
  • 268
0

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
  • 7,656
  • 3
  • 31
  • 45