I stumbled over the following problem that i can\'t extend and implement from this class which is defined in Java 1.5 (java.lang package)
public abstract cla
Enum gets special treatment by the compiler and classes like EnumSet make assumptions based on this treatment. The behavior of Enum methods like ordinal()
and name()
is strictly defined and implementing them yourself to replicate this behavior makes no sense. Also the syntax to declare enum values is only valid in an enum
and wont compile in a class
.
It is important to remember that you should never use ordinal()
yourself, so defining it to suit your problem makes no sense.
You can't extend Enum like that. It's built in to the compiler, or the runtime (not sure which).
But it looks like you're trying to solve the wrong issue; the ordinal of an enum value is not supposed to have any functional meaning. As soon as you give it one, it should have a different name than 'Ordinal'. Your second code snippet is far superior to your first one.
In general, relying on ordinal for anything is bad practice; it probably never should've been exposed in the first place. The only thing you can rely on is the name, and any value you assign yourself.
If it's that important to you to name your field 'ordinal', just use the typesafe enum pattern (item 21), which Enum is just an implementation of.
Enums have a special meaning in the language core and special uses. Thus you can use them in switch
statements, which isn't possible with normal classes.
For example,if you implement it explicitly, you don't have to init the property of "name" and "ordinal" in your constructor. It may cause NPE later.
The Enum
class is special in Java, as it is used to implement the Enum language feature. Thus it is not just a class like any other class in Java, but more special (like String
, for which the +
operator is overloaded, or like the boxed classes). For these features to work, special requirements are needed for enums (like, I guess, consecutive ordinals). Thus the Java Language Specification explicitly forbids manually inheriting from the Enum
class.