How can I extend an existing enum in Java to add new elements?

I want to take an existing enum and add more elements, like this:

enum A {a, b, c}  
enum B extends A {d}  
// B should contain {a, b, c, d}

Is this possible in Java? If not, what are the alternatives to java extend enum while maintaining flexibility?

I’ve worked with enums for quite some time, and one thing I’ve learned is that, while you can’t directly java extend enum, you can get a very similar outcome by using an interface to share behavior across multiple enums. You can make your enums implement a common interface and still maintain flexibility.

For example:

interface CommonEnum {
    String name();
}

enum A implements CommonEnum { a, b, c }
enum B implements CommonEnum { d, e, f }

public class EnumExample {
    public static void main(String[] args) {
        for (CommonEnum value : A.values()) System.out.print(value + " ");
        for (CommonEnum value : B.values()) System.out.print(value + " ");  
        // Output: a b c d e f
    }
}

Why this works: It allows you to treat both enums as a single type while keeping the behavior consistent across different enums. You won’t have the inheritance chain of a java extend enum, but you get that same shared behavior and can even iterate through all values as needed. Pretty neat for cases when you need flexibility but can’t directly extend enums.

I’ve found that combining multiple enum types into one is another effective way to handle the java extend enum limitation. Instead of extending one enum with another, you can group them logically into one enum using an additional field to differentiate between them.

For instance:

enum CombinedEnum {
    A1("A"), A2("A"), A3("A"), // Original A values
    B1("B"), B2("B");          // Extended B values

    private final String category;
    CombinedEnum(String category) { this.category = category; }
    
    public String getCategory() { return category; }
}

Why this approach? By keeping everything in one enum but grouping by a field (like category), you maintain a single source of truth while still logically separating your data. You can still easily differentiate between the original and extended values without the complexity of inheritance. This gives you cleaner code while adhering to the java extend enum limitation.

One more dynamic way I’ve handled the java extend enum issue is by combining multiple enums in a wrapper class. This method works great when you don’t need to modify the enums directly but still want to merge them. I’ve used a Set to store values from both enums dynamically, like this:

import java.util.EnumSet;
import java.util.Set;

enum A { a, b, c }
enum B { d, e }

public class EnumCombiner {
    public static Set<Enum<?>> combinedEnum = EnumSet.noneOf(A.class);
    
    static {
        combinedEnum.addAll(EnumSet.allOf(A.class));
        combinedEnum.addAll(EnumSet.allOf(B.class));
    }

    public static void main(String[] args) {
        System.out.println(combinedEnum); // Output: [a, b, c, d, e]
    }
}

Why is this useful? This approach gives you the ability to combine enums dynamically without needing to modify the original enum types. It also allows you to work with them as a unified collection, which is pretty flexible when you’re dealing with evolving sets of values. It’s a workaround for the java extend enum restriction that can be very useful in certain situations.