47

Possible Duplicate:
add values to enum

Why enums in Java cannot inherit from other enums? Why is this implemented this way?

2
  • stackoverflow.com/questions/55375/add-values-to-enum Commented Dec 10, 2009 at 22:12
  • 3
    I would rephrase you question. Why enums cannot have an abstract base? The accepted answer to the question cited by Bill explains why you cannot extend enums with other values, but it's still not clear to me why they cannot share a base implementation. Commented Dec 10, 2009 at 22:19

1 Answer 1

86

Example stolen from here

Because adding elements to an enum would effectively create a super class, not a sub class.

Consider:

 enum First {One, Two}   
 enum Second extends First {Three, Four}   

 First a = Second.Four;   // clearly illegal 
 Second a = First.One;  // should work

This is the reverse of the way it works with regular classes. I guess it could be implemented that way but it would be more complicated to implement than it would seems, and it would certainly confuse people.

Sign up to request clarification or add additional context in comments.

3 Comments

Probably what you want is First implements MyInterface Second implements MyInterface Then use MyInterface for union of the two groups
Would First a = Second.One be an illegal statement?
@TimothySwan The referenced code won't be compiled, since an exception will be thrown at the enum Second extends First {Three, Four} line.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.