Extending an enum via inheritance Extending an enum via inheritance java java

Extending an enum via inheritance


The reason you can't extend Enums is because it would lead to problems with polymorphism.

Say you have an enum MyEnum with values A, B, and C , and extend it with value D as MyExtEnum.

Suppose a method expects a myEnum value somewhere, for instance as a parameter. It should be legal to supply a MyExtEnum value, because it's a subtype, but now what are you going to do when it turns out the value is D?

To eliminate this problem, extending enums is illegal


When built-in enums aren't enough, you can do it the old fashion way and craft your own. For example, if you wanted to add an additional property, for example, a description field, you could do it as follows:

public class Action {    public string Name {get; private set;}    public string Description {get; private set;}    private Action(string name, string description) {        Name = name;        Description = description;    }    public static Action DoIt = new Action("Do it", "This does things");    public static Action StopIt = new Action("Stop It", "This stops things");}

You can then treat it like an enum like so:

public void ProcessAction(Action a) {    Console.WriteLine("Performing action: " + a.Name)    if (a == Action.DoIt) {       // ... and so on    }}

The trick is to make sure that the constructor is private (or protected if you want to inherit), and that your instances are static.


You're going the wrong way: a subclass of an enum would have fewer entries.

In pseudocode, think:

enum Animal { Mosquito, Dog, Cat };enum Mammal : Animal { Dog, Cat };  // (not valid C#)

Any method that can accept an Animal should be able to accept a Mammal, but not the other way around. Subclassing is for making something more specific, not more general. That's why "object" is the root of the class hierarchy. Likewise, if enums were inheritable, then a hypothetical root of the enum hierarchy would have every possible symbol.

But no, C#/Java don't allow sub-enums, AFAICT, though it would be really useful at times. It's probably because they chose to implement Enums as ints (like C) instead of interned symbols (like Lisp). (Above, what does (Animal)1 represent, and what does (Mammal)1 represent, and are they the same value?)

You could write your own enum-like class (with a different name) that provided this, though. With C# attributes it might even look kind of nice.