I ask this because I have like 10 of them that implement the same interface but they also have the same implementation for some methods so I would like to reuse the code by placing all the same implementations in middle object that extends Enum and it is also the superclass of all the others I need.

5 Answers
5

You can't do it, since the language does not allow you. And for a good logical reason: subclassing an enum would only make sense if you could remove some enum values from the subclass, not add new ones. Otherwise you would break the Liskov Substitution Principle.

This in brief states that every instance of a subclass should be acceptable whenever an instance of a superclass is expected. If you add a new enum member in an enum subclass, that clearly can't be accepted by someone knowing only the super enum.

In your concrete case, @Jason's suggestion may offer a good solution (+1 for him :-)

Update to @OrangeDog's comment

Good point, I was a bit sloppy above :-) Implementation-wise you are right. However, from the logical point of view an enum type is fully described by the set of its valid values. And generally, a proper subclass is a specialization of its superclass. In other words, the set of valid subclass instances is (should be) always a subset of the superclass instance set. (Every dog is an animal, but not every animal is a dog.)

Not strictly true. An enum value is internally an instance of the enum "class". A value from SubEnum could be easily passed as a SuperEnum, and you could call all the methods of SuperEnum on it. You just wouldn't be able to get an instance of SuperEnum that compares equal to it (which is true of identity equality for all classes).
–
OrangeDogJan 5 '11 at 14:25

"And for a good logical reason: subclassing an enum would only make sense if you could remove some enum values from the subclass, not add new ones. Otherwise you would break the Liskov Substitution Principle." That is wrong. Extending an enum would make perfect sence if for instance you created a parent enum with some implemented generic methods.
–
JohanJan 14 '14 at 12:12

1

Note though that you can create anonymous subclasses of enums, as follows: enum E { INSTANCE {} }. E.INSTANCE.getClass() == E.class will evaluate to false. (But I realize that this is usually of little help.)
–
aioobeFeb 13 '14 at 10:04

@Johan, subclassing in general means specializing, in other words restricting the set of possible values in a type. Dog is a subtype of Animal, not the other way around. For enum types, adding new values obviously contradicts this concept. I know this sounds illogical at first (it did to me too), but think it through.
–
Péter TörökFeb 17 '14 at 11:52

I ask this because I have like 10 of them that implement the same interface but they also have the same implementation for some methods so I would like to reuse the code by placing all the same implementations in middle object that extends Enum and it is also the superclass of all the others I need.

Yes. but in case each of the enums has all common attributes, The duplication in constructors and getters and setters is evident. If you coud have a look at my question: stackoverflow.com/questions/31473115/…
–
abksrvJul 18 at 17:10

The language does not allow you to do so, as Enums are designed for representing enumerated values efficiently, not for implementing code. Try using another pattern like a static helper (utility) class.

"not for implementing code" -- Wrong! You can do lots of useful stuff in both the enum class and each of the enum instances. You just can't subclass enums. The Enum framework is designed to produce typesafe singletons.
–
Jason SJan 5 '11 at 14:33

@Jason - What I mean is that the designers' thought process was "we have classes that encapsulate methods, now we need enums to encapsulate enumerated constant values" rather than "we have classes that encapsulate methods, now we need enums to encapsulate methods"
–
OrangeDogJan 5 '11 at 16:04

Ah, got it. in other words enums were designed first and foremost to be safe singletons that encapsulate enumerated constant values, and while they were at it, they put some thought into it to give them some good object-oriented features.
–
Jason SJan 5 '11 at 18:59