Community
Participate
Working Groups
An enum which implements an interface will compile in Eclipse, even if the methods in the interface are not implemented. Invoking the interface methods on one of the enum elements at runtime causes an AbstractMethodError. The Sun compiler correctly reports that the enum needs to implements methods from the interface. Tested against Eclipse 3.1_I20050126-0800 ---- EnumImplementsInterfaceTest.java ---- import junit.framework.TestCase; public class EnumImplementsInterfaceTest extends TestCase { public void testImplementsGenericInterface() { try { E.ITEM.x(); } catch (AbstractMethodError e) { fail(); } } static class X {} interface I { public X x(); } enum E implements I { ITEM() {}; // missing definition of x() } } ---- End of EnumImplementsInterfaceTest.java ----
I have had similar problems lately where I will get AbstractMethodErrors even if I have declared the abstract method in the enum, and the concrete method for each element. Thus far I have not been able to write a test case to capture it precisely. Has anybody else run into this problem?
When the method from the interface is implemented, we still fail with an AbstractMethodError because we don't tag the enum type as abstract. Test case: interface I { public Object foo(); } public enum X implements I { ITEM() { public Object foo() { return null; } }; public static void main(String[] args) { System.out.println(X.ITEM.foo()); } } If I hack the compiler to tag the enum type X as abstract, it runs fine.
If the enum type implements the interface method, it doesn't need to be tagged as abstract. For example, in the following code, X is not abstract. interface I { public Object foo(); } public enum X implements I { ITEM() { public Object foo() { return null; } }; public Object foo() { return "TOTO"; } public static void main(String[] args) { System.out.println(X.ITEM.foo()); } } But if I remove the foo method before the main method, then X is abstract.
We should indeed reject the following: interface I { public Object foo(); } public enum X implements I { ITEM() { }; public static void main(String[] args) { System.out.println(X.ITEM.foo()); } }
Added EnumTest test071-075.
Verified in I20050215-2300