16

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; }
}
jsoc
  • 301
  • 1
  • 4
  • 8
  • What is the complete compile error you get? – Progman Feb 14 '11 at 18:24
  • 12
    Just a guess. Maybe you've checked only the workspace compiler compliance level (set to 6), but your project sets its own to jdk5. – Grzegorz Oledzki Feb 14 '11 at 18:26
  • @Progman - edited question to include the error string – jsoc Feb 14 '11 at 18:43
  • @Grzegorz Oledzki - good suggestion. this project is inheriting the workspace default (6) – jsoc Feb 14 '11 at 18:50
  • 1
    Are you using eclipse 3.7 milestone or nightly build? May it's a regression in latest eclipse. – Kane Feb 16 '11 at 05:26
  • Another guess. Do you have the automatic build turned on? What happens if you perform a workspace-wide clean? – Grzegorz Oledzki Feb 16 '11 at 11:46
  • I have the same problem with ubuntu 10.04 in eclipse (helios). I thought it was because of the open-jdk but i installed sun/oracle 1.6 jdk and configured eclipse to use that (and updated system-wide alternatives to use this as well) and I still get the error when trying to use @Override for interface methods.. Any luck figuring this out? – Matt Wolfe Apr 30 '11 at 06:51
  • The fix for me was to change the compiler complience level to 1.6 as has already been suggested. The trick was figuring out the correct way to do it. I had never setup custom settings for this project, and the default was set to 1.6 so I incorrectly assumed my project must be 1.6 as well. After clicking the "Configure project specific settings" button, I found that it was set to 1.5. After changing to 1.6 the problem was fixed for me. – Matt Wolfe Apr 30 '11 at 07:06
  • An update on my specific problem (still unresolved for the version of JDK, eclipse, and Linux I was using): workspace default set to 1.6, project specific settings set to inherit from workspace (or set to 1.6 explicitly) and I still got compiler errors. My (unfortunate) workaround at the time was to remove @Override declarations for the methods in my concrete class that implemented the interface. I've since moved on to a new dev environment (newer JDK, eclipse, Mac OS) that does not have this problem. – jsoc Nov 25 '13 at 23:58

8 Answers8

14

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?

Asaph
  • 159,146
  • 25
  • 197
  • 199
  • I suspect that you're right, but where do I check for this within eclipse? The compiler compliance level within eclipse is already set to 6.0 (1.6) – jsoc Feb 14 '11 at 20:52
  • 2
    For Eclipse: right button on project -> Properties... > Java compiler, then configure as you want – M3rlino Feb 24 '11 at 11:25
4

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

Chowdappa
  • 1,580
  • 1
  • 15
  • 31
3

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>
Jaime Garza
  • 483
  • 1
  • 4
  • 10
3

In eslipse can use different versions of compilers.

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

splean
  • 31
  • 1
  • 1
1

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.

Edub Kendo
  • 473
  • 2
  • 11
0

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

jdowdell
  • 1,578
  • 12
  • 24
0

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

Steven Schlansker
  • 37,580
  • 14
  • 81
  • 100
  • Added code sample. Note: code compiles fine in other configurations. I think it has something to do with version of jdk I'm running within linux, or something about my linux eclipse config that I'm just failing to see. – jsoc Feb 14 '11 at 19:07
0

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

pjaol
  • 173
  • 1
  • 5