Reputation: 12434
I need to make a Java class which can receive a single enum value out of many. For example:
public class MyClass
{
public enum enumA {..}
public enum enumB {..}
public enum enumC {..}
public enum enumD {..}
private OneOfTheEnumsAboveMember enumMember;
}
Since enums can't be extended, how can I define a single member which must be one of the enums?
Upvotes: 5
Views: 61
Reputation: 311843
Enums cannot be extended, but they can implement interfaces. You could have an empty marker interface (or better yet - have it include methods you actually need), and make that the type of your member:
public class MyClass {
public static interface EnumInterface {}
public enum enumA implements EnumInterface {..}
public enum enumB implements EnumInterface {..}
public enum enumC implements EnumInterface {..}
public enum enumD implements EnumInterface {..}
private EnumInterface enumMember;
}
Upvotes: 6