Reason why enums since JDK1.5 cannot inherit

O

Olli Plough

Hi,

am a bit annoyed because enums since JDK1.5 apparently cannot inherit
from other enums. Problem is I have to plaster code like

public enum MyEnum
{

private final String value;

Role(String value)
{
this.value = value;
}

public String value()
{
return value;
}
}

repeatedly into my application. Would be nice if the repetitive code
could be factored out into some abstract class. Am I getting something
wrong here or do I have to live with this?

Regards, Oliver Plohmann
 
P

Piet Blok

Hi,

am a bit annoyed because enums since JDK1.5 apparently cannot inherit
from other enums. Problem is I have to plaster code like

public enum MyEnum
{

private final String value;

Role(String value)
{
this.value = value;
}

public String value()
{
return value;
}
}

repeatedly into my application. Would be nice if the repetitive code
could be factored out into some abstract class. Am I getting something
wrong here or do I have to live with this?

Regards, Oliver Plohmann

Oliver,

I don't know why (presumably there is a good reason), but I share your
annoyance.

In my own code base I tried to circumvent this "short coming"
by introducing a "PseudoEnum" class, that behaves as much as possible
like an enum, but is in fact an ordinary class and, as such,
extensible.

http://www.pbjar.org/docs/src/org/pbjar/resources/PseudoEnum.java

From the API documentation you may find some possible use cases.

http://www.pbjar.org/Docs/API-docs/index.html

Hope this helps

Piet
 
M

Mark Space

Olli said:
Hi,

am a bit annoyed because enums since JDK1.5 apparently cannot inherit
from other enums. Problem is I have to plaster code like

Just trying to think outside the box here a bit....

Have you looked into the EnumSet and the EnumMap classes? They are
supposed to be as fast as bit operations and array indexes for most
(presumably, relatively small, ie < 32 members) Enumerations. They
should be faster that HashSet/HashMap.

Put your "extra" information into a lookup EnumMap with the key being
the Enum and your string info being the value. Then you can hopefully
only declare your extra-info class once and reuse it every where.

This is basically late binding but this could have some advantages too.
If you need to go the other way (look up the associated enum given the
value) just embed the enum into each class.

You might get away with instantiating this EnumMap once, per Enum, in
the declaration of the Enum class itself. Depends on your design...

Suggestion: put your String information into a generic extra info class.
Parameterized it with an Enum (ie, class MyClass<ENUM> ... ) and then
you've got one class type per enum type to fool around with.
 
O

Olli Plough

Suggestion: put your String information into a generic extra info class.
Parameterized it with an Enum (ie, class MyClass<ENUM> ... ) and then
you've got one class type per enum type to fool around with.

This is a good idea! Thanks.

Oliver Plohmann
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top