Can a Java enum implement an interface? How, and why is that useful?
answer
- Enum can't extend a class (already extends Enum) but can implement interfaces
- implements after the enum name; methods shared or per-constant
- Enum-as-Strategy: callers depend on the interface
- Extensible-enum pattern: interface + multiple enums implementing it
- Comparable & Serializable already implemented implicitly
basics
~10 sYes. An enum can use implements SomeInterface and provide the interface methods, either once for the whole enum or per constant. It cannot extend a class, but it can implement any number of interfaces.
solid answer
~40 sA Java enum cannot extend another class (it already implicitly extends java.lang.Enum), but it can implement one or more interfaces. You add `implements Foo, Bar` after the enum name and supply the methods — either a single shared implementation in the enum body, or constant-specific bodies so each constant implements them differently. This is the standard way to let a set of related constants act as interchangeable strategies behind a common type: callers depend on the interface, and any constant can be passed wherever the interface is expected. A common pattern is the 'extensible enum' where multiple enums all implement the same interface (e.g. an Operation interface) so client code accepts the interface and works across enum families. Enums also already implement Comparable and Serializable implicitly, which is why you never declare those.
code
java · 11 linesinterface Op { double apply(double a, double b); }
enum BasicOp implements Op {
PLUS { public double apply(double a, double b) { return a + b; } },
MINUS { public double apply(double a, double b) { return a - b; } };
}
void demo() {
Op op = BasicOp.PLUS; // a constant is-a Op
double r = op.apply(2, 3); // 5.0, via the interface
}go deeper
Knows an enum can say implements SomeInterface and that it can't extend a class.
Can implement an interface on an enum, shared or per-constant, and explain enum-as-strategy.
Explains the extensible-enum pattern, the implicit Comparable/Serializable, and why interfaces are the right decoupling seam for constant families.
Designs APIs around the interface (not the enum) for evolvability, reasons about bounded generics like <T extends Enum<T> & Op>, and the trade-off vs sealed classes/records.
## Background: what an enum extends Every Java `enum` **implicitly extends `java.lang.Enum`**. Java has single inheritance of *implementation*, so since an enum already has a superclass, **an enum can never `extends` another class**. What it *can* do — like any class — is **implement interfaces** (Java allows multiple inheritance of *type* through interfaces). An **interface** is a contract of method signatures with no required state; a class (or enum) that `implements` it promises to provide those methods. ## Syntax ```java interface Describable { String describe(); } enum Severity implements Describable { LOW, MEDIUM, HIGH; public String describe() { return "severity=" + name(); } } ``` Now `Describable d = Severity.HIGH;` is legal — a constant *is-a* `Describable`. You can pass `Severity.HIGH` to any method accepting `Describable`. ## Two ways to implement the methods 1. **One shared implementation** in the enum body (as above) — every constant behaves the same way, perhaps using `name()`/fields to vary the result. 2. **Per-constant (constant-specific) implementations** — combine with constant bodies so each constant implements the interface differently: ```java interface Op { int apply(int a, int b); } enum Arith implements Op { ADD { public int apply(int a, int b) { return a + b; } }, SUB { public int apply(int a, int b) { return a - b; } }; } ``` The interface method can be left implicit-abstract (the interface already declares it) and each constant supplies a body. ## Why it's useful: enum as a Strategy Because the constants share a common interface type, an enum becomes a clean, type-safe **Strategy** set: a fixed roster of named strategies that callers select among. Client code depends only on the interface (`Op`), so it is decoupled from the concrete enum. ## The 'extensible enum' pattern Enums can't be subclassed to add constants, which limits extensibility. The recommended workaround (Effective Java) is to **define the interface, then have one or more enums implement it**. Client APIs accept the *interface*, so a second enum can add more 'operations' later and still flow through the same code: ```java interface Op { double apply(double a, double b); } enum BasicOp implements Op { /* PLUS, MINUS ... */ } enum ExtendedOp implements Op { /* EXP, REMAINDER ... */ } // works for both enums: static <T extends Enum<T> & Op> void run(Class<T> opType) { ... } ``` ## Implicit interfaces you get for free Every enum **already implements `Comparable`** (ordering by declaration order / `ordinal`) and **`java.io.Serializable`**. So you never declare these and shouldn't override `compareTo` (it's `final` on `Enum`). They are the reason enums slot straight into `TreeSet`/`TreeMap` and sort without extra work.
- Why can an enum implement interfaces but not extend a class?Java has single implementation inheritance and every enum already extends java.lang.Enum, so its one superclass slot is used. Interfaces are multiple-inheritance of type, so an enum can implement as many as it likes.
- What does the 'extensible enum' pattern look like and what problem does it solve?Define an interface, have several enums implement it, and write client code against the interface. Since enums can't be subclassed to add constants, this lets a new enum supply more constants that still work with existing code.
An enum is like a sealed box of pre-built tools; making it implement an interface puts a standard handle on each tool so any of them fits the same socket the caller holds.
saying these in an interview costs you the question
- Saying an enum can extend another enum or class — it cannot.
- Declaring `implements Comparable, Serializable` on an enum — already provided, and compareTo is final.
- Thinking each constant needs the same interface implementation; per-constant bodies are allowed.