Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Reason why enums since JDK1.5 cannot inherit

Thread view: 
Olli Plough - 16 Dec 2007 13:53 GMT
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
Piet Blok - 16 Dec 2007 15:42 GMT
Olli Plough <oliver@plohmann.com> wrote in news:6b5abf63-978a-49da-8fe0-
5deee7b94743@e67g2000hsc.googlegroups.com:

> Hi,
>
[quoted text clipped - 22 lines]
>
> 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
Lew - 16 Dec 2007 19:48 GMT
Olli Plough <oliver@plohmann.com> wrote "
>> am a bit annoyed because enums since JDK1.5 apparently cannot inherit
>> from other enums. Problem is I have to plaster code like

This is documented behavior.  If you need different behavior, the type-safe
enum pattern can be used with a custom class.

Joshua Bloch's /Effective Java/, Item 21, discusses the pattern.  I wonder if
it'll be excised from the next edition now that Java has enum?  It shouldn't be.

<http://www.bearcave.com/software/java/misl/enum/type_safe_enum.html>
for an implementation.

Signature

Lew

Mark Space - 16 Dec 2007 23:41 GMT
> 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.
Olli Plough - 17 Dec 2007 13:35 GMT
> 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
Roedy Green - 18 Dec 2007 01:50 GMT
On Sun, 16 Dec 2007 05:53:42 -0800 (PST), Olli Plough
<oliver@plohmann.com> wrote, quoted or indirectly quoted someone who
said :

>am a bit annoyed because enums since JDK1.5 apparently cannot inherit
>from other enums.

the key to understanding enums is to see how they are implemented as
ordinary classes under the hood.  All the weird rules derive from that
implementation.   See http://mindprod.com/jgloss/enum.html
to explain how the work.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.