• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Enum Type inhteritance

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Start to use JDK 1.5 recently, and notice that I couldn't do certain things with enum, hope someone give me a pointer.

Let me explain what I was trying to do:

I have two kind of types: ItemType and NodeType both has two fields, Id, and Type. I defined a interface Long getId() and String getType()

I was hoping defined a common class that implements getId() and getType()
and let type class to inherited from it.

This can be easily done in a class type.

But If also want to use the new enum feature, then I got into trouble:

The enum does not allow extends and it does not like to extends from an class type. So I can't do the following:




what I can do is define the getType and getId in the enum class



The problem with this is that I have to implement the same getId and getType methods for NodeType or any other enum types I care to add.

How to achieve Enum Inheritance ? any technique some one could share ?

thanks

Chester
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's simply not possible, sorry.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

is converted into a class like this:

Note that enum can implement interfaces.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

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!

cheers and many thanks,

Chris
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

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.
 
Chris G. May
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...


 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can understand where you're coming from, but I don't think changing a language to support a meta-language is a good idea. The annotation mechanism should be changed instead.
 
Chris G. May
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The annotation mechanism should be changed instead.


Hmmm, how would you change the annotation mechanism without changing the language?...
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Happily living in the valley of the dried frogs with a few tiny ads.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic