MC Emperor MC Emperor - 4 years ago 155
Java Question

Can I make an abstract enum in Java?

Below is a valid enum declaration.

public enum SomeEnumClass {

ONE(1), TWO(2), THREE(3);

private int someInt;

public SomeEnumClass(int someInt) {
this.someInt = someInt;
}
}


But can I override an abstract class with an enum type?

SomeEnumClass.java

public abstract enum SomeEnumClass {

private int someInt;

public SomeEnumClass(int someInt) {
this.someInt = someInt;
}
}


OverridingEnumClass.java

public enum OverridingEnumClass extends SomeEnumClass {

ONE(1), TWO(2), THREE(3);

}


If not, why not? And what is a good alternative?

Answer Source

No, you can't; enum types all extend Enum, and they're implicitly final. Enums can implement interfaces, or you can declare the relevant methods directly on the enum class in question.

(I do see the basic idea of what you want, which is a mixin; perhaps the Java 8 interfaces will be a bit more useful in this regard.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download