Java – How to use java.lang.Enum

java

This is not a question of the new features in enum.
This is not a question that waits for an answer that says
enum is equivalent to class xyz extends Enum. If you wish to preach on how enums are used, as found in numerous tutorials, please don't answer.

This question is specifically on how to use the class java.lang.Enum.
Rather it is true that java.lang.Enum cannot be directly extended nor instantiated, right?
It is true that java.lang.Enum is only for the benefit of enum type and nothing else right?

If so, the variable colours is pretty meaningless, right? Even though the compiler allows it.

final static Enum<colors> colours = null;
enum colors {"R","G","B"};

So, what could I possibly equate the variable colours besides null.

of course, compiler won't allow me to do this

class BaseColour extends Enum<BaseColour>{ ,,,}

but it allows enum an unfair advantage to able to do it as a compiler's behind-the-curtains manipulation.

I am asking this question because I wish to do something like these

class Reddish extends BaseColour {"Crimson", "Blood", "Pink"};
class Purplish extends BaseColour {"Violet", "Evening Sky", "Brinjal"};
class Greenish extends BaseColour {"Algae", "Leaf"};

I wish to pass any of these, and only any of these, enumerated types into a method

void squeezeColour(BaseColour colourConstraint)
{ ... }

Is it possible?
/** If not, and if you happen to be James Gosling et al, would you kindly consider this for the next version of Java specs? **/

Further addition to question: I forgot to say this …
What I am working on is using an EnumMap or EnumSet and exploiting the contains and range methods. Is that the best I could do?

Best Answer

What an enum actually is, internally, is an integer constant. Or at least, the JVM is free to treat it as one, precisely because of the kind of restrictions that are frustrating you. That makes enums efficient, at the cost of breaking object-orientation.

I suggest you revert to the old "type checked enum" pattern that was used before enums had syntax.

class Foo {

  // the constants
  public static final Foo RHUBARB = new Foo();
  public static final Foo CUSTARD = new Foo();

  // only this class and subclasses can create constants
  protected Foo(){ }
}