50

Below is a valid enum declaration.

public enum SomeEnumClass {

    ONE(1), TWO(2), THREE(3);

    private int someInt;

    public SomeEnumClass(int someInt) {
        this.someInt = someInt;
    }
}

But can I override an abstract class with an enum type?

SomeEnumClass.java

public abstract enum SomeEnumClass {

    private int someInt;

    public SomeEnumClass(int someInt) {
        this.someInt = someInt;
    }
}

OverridingEnumClass.java

public enum OverridingEnumClass extends SomeEnumClass {

    ONE(1), TWO(2), THREE(3);

}

If not, why not? And if not, then what is a good alternative?

5
  • enum != class. You can't write abstract enum Nov 30, 2013 at 10:24
  • 2
    @MaximShoustin In what way is an enum not a class? See docs.oracle.com/javase/7/docs/api/java/lang/Enum.html.
    – MC Emperor
    Oct 1, 2014 at 22:10
  • c'mon, don't mix enum with Enum. :) Oct 1, 2014 at 22:40
  • 1
    @MaximShoustin Then what's the difference?
    – MC Emperor
    Oct 2, 2014 at 8:27
  • 2
    @MCEmperor Fortunately, enum isn't any magic type. They are just classes extending Enum. Doing javap classfile helps demystify. Aug 5, 2018 at 14:15

3 Answers 3

48

No, you can't; enum types all extend Enum, and they're implicitly final. Enums can implement interfaces, or you can declare the relevant methods directly on the enum class in question.

(I do see the basic idea of what you want, which is a mixin; perhaps the Java 8 interfaces will be a bit more useful in this regard.)

1
  • 11
    Ah, fortunately, an enum can implement an interface. So I used that as an alternative.
    – MC Emperor
    Jun 24, 2014 at 22:30
12

In java you can't extend enum or create some abstract enum or even generify enum. If you want to have some polymorphic extension point over your enum you can apply such a pattern.

Lets say your enum

public enum SomeEnumClass {
    ONE, TWO, THREE;
}

And you want to have some behavior associated with each value. But you don't want to hardcode each, but rather want to have ability to supply any other. So you should declare interface

public interface SomeEnumVisitor<P, R> {
     R one(P param);
     R two(P param);
     R three(P param);
}

Then add abstract method to enum declaration and implementation of this method for each value

public enum SomeEnumClass {
    ONE {
        @Override
        public <R, P> R accept(SomeEnumVisitor<R, P> visitor, P param) {
            return visitor.one(param);
        }
    }, TWO {
        @Override
        public <R, P> R accept(SomeEnumVisitor<R, P> visitor, P param) {
            return visitor.two(param);
        }
    }, THREE {
        @Override
        public <R, P> R accept(SomeEnumVisitor<R, P> visitor, P param) {
            return visitor.three(param);
        }
    };
    public abstract <R, P> R accept(SomeEnumVisitor<R, P> visitor, P param);
}

So that you can create visitor implementation for extending your enum behavior. For example in your case you wanted to associate Integer value with each enum value.

public class IntValueVisitor implements SomeClassVisitor<Integer, Void> {
     @Override
     public Integer one(Void param){
         return 1;
     }

     @Override
     public Integer two(Void param){
         return 2;
     }

     @Override
     public Integer three(Void param){
         return 3;
     }    
}

And finally use this visitor where you need

SomeClassEnum something = getAnyValue();
// this expression will return your particular int value associated with particular enum.
int intValue = something.accept(new IntValueVisitor(), null);

Of course this pattern applicable if it is not appropriate to have everything declared inside enum, for example if you have enum declaration in library and want to extend enum's behavior in the main application. Or you just don't want to couple enum definition and implementation details.

In order to simplify this pattern implementation there is a library that can generate enum and visitor based on annotation so that all you need to declare in your code is

@AutoEnum(value = {"one", "two", "three"}, name = "SomeEnumClass")
public interface SomeEnumMarker {}

The tool will do rest for you.

1
  • To add on top of this answer, no abstract methods are required to have the constant specific class body to override the current methods. This means you could keep the default implementation of your method, and only override the ones you need to specially treat. Nov 20, 2018 at 1:26
6

If you really need to "extend an enum", you could use the the pre-Java 1.5 Typesafe Enum Pattern (see the bottom of http://www.javacamp.org/designPattern/enum.html ) which actually uses a class, not an enum. You lose the ability to use the EnumSet with your "enum"s and you lose some auto-generated methods such as items(), but you get the ability to override methods.

An example:

// Typesafe enum pattern
public static abstract class Operator {
    public static final Operator ADD = new Operator("+") {
        @Override
        public Double apply(Double firstParam, Double secondParam) {
            return firstParam + secondParam;
        }
    };
    public static final Operator SUB = new Operator("-") {
        @Override
        public Double apply(Double firstParam, Double secondParam) {
            return firstParam - secondParam;
        }
    };
    public static final Operator MUL = new Operator("*") {
        @Override
        public Double apply(Double firstParam, Double secondParam) {
            return firstParam * secondParam;
        }
    };
    public static final Operator DIV = new Operator("/") {
        @Override
        public Double apply(Double firstParam, Double secondParam) {
            return firstParam / secondParam;
        }
    };

    private static final Operator[] _ALL_VALUES = {ADD, SUB, MUL, DIV};
    private static final List<Operator> ALL_VALUES = Collections.unmodifiableList(Arrays.asList(_ALL_VALUES));

    private final String operation;

    private Operator(String c) {
        operation = c;
    }

    // Factory method pattern
    public static Operator fromToken(String operation) {
        for (Operator o : Operator.items())
            if (o.operation.equals(operation))
                return o;
        return null;
    }

    public Iterable<Operator> items() {
        return ALL_VALUES;
    }

    public abstract Double apply(Double firstParam, Double secondParam); 

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.