HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaCritical

Can enums be subclassed to add new elements?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
subclassedenumsaddcannewelements

Problem

I want to take an existing enum and add more elements to it as follows:

enum A {a,b,c}

enum B extends A {d}

/*B is {a,b,c,d}*/


Is this possible in Java?

Solution

No, you can't do this in Java. Aside from anything else, d would then presumably be an instance of A (given the normal idea of "extends"), but users who only knew about A wouldn't know about it - which defeats the point of an enum being a well-known set of values.

If you could tell us more about how you want to use this, we could potentially suggest alternative solutions.

Context

Stack Overflow Q#1414755, score: 502

Revisions (0)

No revisions yet.