Extending an enum via inheritance

The reason you can’t extend Enums is because it would lead to problems with polymorphism.

Say you have an enum MyEnum with values A, B, and C , and extend it with value D as MyExtEnum.

Suppose a method expects a myEnum value somewhere, for instance as a parameter. It should be legal to supply a MyExtEnum value, because it’s a subtype, but now what are you going to do when it turns out the value is D?

To eliminate this problem, extending enums is illegal

Leave a Comment