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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With