The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

Java's enum does not allow inheritance, as you already found out. There's no way to hack around it. An enum is essentially converted to a final class by the compiler, which means that it can't be extended. An enum like this:

the missing inheritance of enumerations is a recurring (and pretty much arbitrary) limitation of the Java language, and for many instances a royal pain in the neck (especially in combination with annotations).

We are joining forces now to propose at least a basic support for enumeration extendability for the upcoming Java-8 edition.

Please help us in moving the Extended-Enum petition forward, and vote for us at http://www.extended-enums.org - this would be really minimal change with a maximum of impact and transcendence!

Personally, I see no point in extensible enums. The other way around, enums being able to extend base classes other than Enum may be interesting, but as the author notes, this can easily be replaced with delegation.

Generics for enums make no sense at all. The point of enums is that they are well defined lists of constants that are known ahead of time. If they are known ahead of time, what's stopping you from inserting the type arguments when you define them, effectively removing generic parameters? If they are not known ahead of time, then why are you using enums in the first place?

The author mentions extending enums to allow for new enum constants that were not known at the time of conception of the base enum. If there was a need for this, then that means the original API was degenerate, and should have used an interface instead of an enum.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.

Chris G. May
Greenhorn

Joined: Jun 25, 2011
Posts: 3

posted Jun 26, 2011 04:26:27

0

Stephan van Hulst wrote:The point of enums is that they are well defined lists of constants that are known ahead of time.

Well, actually there are many cases where the list of constants is *not* known ahead of time. I specifically talk about frameworks, which are extendible by definition, and where you need an (abstract) placeholder, because you do not know which values will be filled in by the implementor

Stephan van Hulst wrote:...and should have used an interface instead of an enum.

I wish I could! Unfortunately inside annotations you cannot use interfaces (or regular objects for that matter), only primitive types, Strings, enums and other annotations are allowed.

To make it clear: My problem is really focused (if not limited) to using enumerations in combination with annotations. This seems to be an exotic use case, but if you work extensively with annotations as I do (as kind of meta-programming, see also www.soplets.org) then you will really feel the pain...

Ahh you have to excuse me there. I regard annotations as something separate from Java, as they don't (or shouldn't at least) tell a program how it's supposed to behave, but they tell other programs something about this program.

When I say I'm not okay with changing the language to accommodate annotations, I mean the non-reflective part of the language.