GeeCON Prague 2014*
The moose likes Java in General and the fly likes Enum Type inhteritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Enum Type inhteritance" Watch "Enum Type inhteritance" New topic
Author

Enum Type inhteritance

Chester Chen
Greenhorn

Joined: Feb 02, 2006
Posts: 1
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I think it's simply not possible, sorry.


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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Chris G. May
Greenhorn

Joined: Jun 25, 2011
Posts: 3
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
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Jun 25, 2011
Posts: 3
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

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Jun 25, 2011
Posts: 3
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

Joined: Sep 20, 2010
Posts: 3647
    
  16

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.
Vlad Yatsenko
Greenhorn

Joined: Aug 15, 2011
Posts: 2
Extending enums explained: http://www.vlad-yatsenko.eu/2011/01/java-magic-extending-enumerations/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Welcome to the Ranch Vlad Yatsenko
Vlad Yatsenko
Greenhorn

Joined: Aug 15, 2011
Posts: 2
Campbell Ritchie wrote:Welcome to the Ranch Vlad Yatsenko
Thanks for the friendly greeting.
P. S. Sorry for the off-topic.
 
GeeCON Prague 2014
 
subject: Enum Type inhteritance