Save one enumeration value from many types

I need to create a Java class that can get one enumeration value out of many. For instance:

public class MyClass { public enum enumA {..} public enum enumB {..} public enum enumC {..} public enum enumD {..} private OneOfTheEnumsAboveMember enumMember; } 

Since enumerations cannot be extended, how can I define one element that should be one of the enumerations?

+5
source share
1 answer

Enumerations cannot be extended, but they can implement interfaces. You can have an empty token interface (or better yet - let it include the methods you really need) and make this 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; } 
+6
source

Source: https://habr.com/ru/post/1244418/


All Articles