10/25/2006

Java Enum and Its Superclass

All java enum E implicitly extends java.lang.Enum<E>. Since java doesn't allow multiple inheritance, enum types can't have superclass. They can't even extend from java.lang.Enum, nor java.lang.Object. It also means enum A can't inherit or extend enum B.

For example, the following is an invalid enum declaration:

public enum MyType extends Object {
ONE, TWO
}
Compiler error:
MyType.java:3: '{' expected
public enum MyType extends Object {
MyType.java:6: identifier expected
2 errors
The correct form should be:
public enum MyType {
ONE, TWO
}
More posts about java enum:
Tags: ,

16 comments:

Anonymous said...

Wow, this is a truly pointless article.

Anonymous said...

I agree! And yet I got here through a simple google search for "java enum." Google better improve their search algorithm so people don't reach pages like these.

Anonymous said...

It's not pointless. It just answers to my question: It's not possible to define a superclass for an enum.

Anonymous said...

you suck!

Anonymous said...

Guys, knowledge transfer, or his willigness to do it needs to be appreciated, no matter whether its useful to not.

Anonymous said...

I agree that this is useful info. I could've wasted more time trying to extend some of my other class.. This article just helped me not to do that.. I appreciate your post..

Anonymous said...

Would like to make four points:

1. First this blog answered the question point-blank on why enums cannot be extended. I was wondering why and I now know.

2. This article is very short and to the point. And thats the point !!. Do these complaining folks need a bulky Gartner Report to explainh that point. Huh ? Perhaps so...

3. People who posted their response here saying this article is useless have spent more time posting their response than it takes to read this article. Apparently they have lot of time on their hands to waste. So what is useless - this article or their time?

4. People complaining that Google change the search algorithm just so that this article doesnt show up dont know anything how search works at all. If ignorance is bliss for you, so be it !!

Anonymous said...

Thanks for explaining the point (why enum cant extend even an Object) so well and with examples. I appreciate it.

Anonymous said...

Very helpful. Thank you!

Chris May said...

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 thnx,

Chris

Anonymous said...

you guys are dumb.

because: actually, this article just answered my question in about 2 sentences.

Anonymous said...

which brings up another point: english sentences allow for Java Object Names?? java.lang.Object? Really?? I don't think that periods are allowed to be in the middle of words. Therefore, all Java language classes, must be discussed without breaking english language rules.. So.. Java is like a baby, who notes that his diaper is crappy, because he crapped in it. I think Java could work a little harder to do some language-construct bending on the Enum thing.

Anonymous said...

for example: enum does not provide accurate abstraction, but it makes mention of the fact that they were trapped.

Anonymous said...

The article doesn't provide a wealth of information, but I still got the useful information of the existence of java.lang.Enum. That's what I needed for building my generic method...

Anonymous said...

Very important to me.

Anonymous said...

3rd class stuff