@Override compile error, implementing an interface

2019-02-12 04:23发布

问题:

I am getting compile errors in eclipse when using the @Override annotation for a class that is implementing an interface.

Compiler compliance level is set to Java 6.0.

I am using the latest version of the 6.0 jdk.

Error: "The method {methodname} of type {classname} must override a superclass method"

Same code works fine on mac with comparable configuration.

public interface ChannelIF {
...
    public boolean canSendNarrowcast();
    public boolean canSendBroadcast(); 
}

public class FacebookChannel implements ChannelIF 
{
...
    @Override
    public boolean canSendNarrowcast() { return true; }

    @Override
    public boolean canSendBroadcast() { return true; }
}

回答1:

This feature is only valid in Java 6 and higher. I see you are using jdk 1.6. That's good. Possible cause: You are compiling with -source 1.5. Is this the case? If so, can you change it to -source 1.6?



回答2:

I too have facing problem and just resolved. Change "Compiler compliance level" to 1.6 in Project->right click->properties->Java compiler.



回答3:

In eslipse can use different versions of compilers.

See сonfiguration in eclipse Preference->Java->Compiler "Compiler compliance level". You must choose "1.6".



回答4:

I just found that when using maven I needed to also modify the pom.xml to have the compiler plugin. I had all my settings properly specified, but I needed this one:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <inherited>true</inherited>
      <configuration>
          <source>1.6</source>
          <target>1.6</target>
      </configuration>
    </plugin>
  </plugins>
</build>


回答5:

So, I kept having this problem, and after running through the above solutions a few times, seeing that my version of Java was set to 1.7 all the way down, not using Maven, and so just quitting and restarting Eclipse a couple times as @jdowdell suggested, after which, it would seem to work until the next time I implemented one of these. I realized that when I was re-starting Eclipse it was prompting me to save my files and I realized that my interfaces had not been saved, so the original method, the one being overrode, didn't exist on disk. And this is why quitting and restarting Eclipse fixes the problem.

tl;dr : Check that all your files are saved.



回答6:

Sounds like your method signatures don't match. They must be the same, including things such as thrown checked exceptions. Post your code!



回答7:

Check the Runtime library, the compiler settings are probably different

Right click on the project name, properties, Java Build Path, Libraries

Look at the version of JRE System Library, more than likely it's set to 1.5 or lower. Select JRE System library, then click remove Click Add Library, select JRE System Library, Next Then either Workspace Default, or



回答8:

I had this same problem for about an hour, and then it seemed to mysteriously solve itself. I wish I know what I did. I had started with workspace settings set for 1.6, unwittingly having project settings set to 1.5 compatibility. I discovered this and changed project compiler settings to 1.6, which resolved some errors, but not the @Override issue for interfaces. Some combination of the following fixed it:

  1. Changing the jre back from jdk to the jre that Eclipse custom installed (or somebody, wasn't me).
  2. Disabling project specific settings in the compiler section, and also clicking Reset to Defaults, on both project and workspace settings for compiler.
  3. Quitting Eclipse and rerunning it as Administrator (I copied eclipse into Program Files on Windows from a .zip, not from an installer; and I think it can't change its config files being a child of that folder without being run as administrator - but I could be dead wrong).

One fellow apparently solved the issue via complete uninstall and reinstall of all java/eclipse related components: http://p2p.wrox.com/book-beginning-android-application-development/87299-override-errors-despite-1-6-a.html