I am wondering why in the Java language a class
cannot extend an enum
.
I\'m not talking about an enum
extending an enum<
You can't extend an enum
. They are implicitly final
. From JLS § 8.9:
An enum type is implicitly
final
unless it contains at least one enum constant that has a class body.
Also, from JLS §8.1.4 - Superclasses and Subclasses:
It is a compile-time error if the
ClassType
names the classEnum
or any invocation of it.
Basically an enum
is an enumerated set of pre-defined constants. Due to this, the language allows you to use enums in switch-cases
. By allowing to extend them, wouldn't make them eligible type for switch-cases, for example. Apart from that, an instance of the class or other enum extending the enum would be then also be an instance of the enum you extend. That breaks the purpose of enum
s basically.