11

Eclipse indigo, java 1.6

public interface I {
  String getName();
}

/* and in another file */

public enum E implements I {
  E1() {
     String getName() { return "foo"; }
  };
}

In Eclipse, this worked! Other classes could invoke getName() on references of type I. The actual javac rejected it, claiming that there was no such thing as getName() in the enum. Is this just an Eclipse bug?

Note that what's wierd about this is the method definition inside the enumerator. It all works just fine in both Eclipse and Javac if I do the normal thing, and have the function defined at the bottom of the enum returning the value of a field.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • 1
    It should be allowed. This question is related: http://stackoverflow.com/questions/2709593/why-would-an-enum-implement-an-interface What `javac` error are you getting? – Mike Kwan Mar 07 '12 at 18:11
  • 2
    Also related to http://stackoverflow.com/questions/9505857/workaround-for-javac-compilation-order-bug-in-maven – Nicocube Mar 07 '12 at 18:13
  • Just checked this in `Eclipse Indigo SR1`. Doesn't compile as expected. – jFrenetic Mar 07 '12 at 18:19
  • 3
    Just curious. Does this have so many upvotes, just because no one knew that enum could implement an interface in Java, or because of a title? I think both :) – jFrenetic Mar 07 '12 at 18:22
  • I didn't know enum constants could have class bodies until a week ago, when someone filed a bug report that my decompiler didn't handle them. Wouldn't surprise me if a lot of people learn something new from this question :). – Mike Strobel Jun 20 '13 at 20:19

4 Answers4

13

getName() in E1 should be public -- is that what's causing you problems? Otherwise, you're trying to override a public method (all methods declared in interfaces are public) with a package-private method, which isn't allowed.

yshavit
  • 42,327
  • 7
  • 87
  • 124
1

Interfaces methods are public scoped. Increase the visibility level in your enum and it should compile successfully. As a side note, your code shows a compilation error in my version of Eclipse (Indigo running on Mac 0S X 10.7.2, JDK 1.6).

Perception
  • 79,279
  • 19
  • 185
  • 195
1

First I agree with @yshavit.

Otherwise it can be related with this one: Workaround for javac compilation order bug in maven

I think it's name order related. Try to rename your interface A, it may compile first and things should work.

Community
  • 1
  • 1
Nicocube
  • 2,962
  • 2
  • 20
  • 28
0

Keep in mind that Eclipse implements its own parser and compiler, which provides output that tightly binds to it's editor. Normally, it works very well; however, when a new language feature comes into play, the compiler tends to lag behind in implementing the new language feature.

The javac command line is (very nearly) always right, assuming you are getting your javac from Oracle / SUN.

yshavit's answer is best, as it identifies the reason why it shouldn't compile in Eclipse. By now I imagine it's been fixed (and properly won't compile in Eclipse).

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138