Explain Codes LogoExplain Codes Logo

Why would an Enum implement an Interface?

java
strategy-pattern
enum-implementation
interface-implementation
Nikita BarsukovbyNikita Barsukov·Mar 14, 2025
TLDR

In Java, enums can implement interfaces to encapsulate functionality within constants, similar to class instances. With adherence to a common interface, it's guaranteed that enums will provide certain methods, promoting consistency and interoperability.

public interface Computable { double compute(double x); } public enum MathConstant implements Computable { PI { public double compute(double x) { return x * Math.PI; } // Because why not enjoy a slice of π? 🥧 }, E { public double compute(double x) { return x * Math.E; } // E-njoy your calculations! } } // Usage double circumference = MathConstant.PI.compute(2); // double the PI, double the fun!

This pattern allows for type-safe operations, making enums more meaningful by participating in your application's logic, rather than serving as mere lists of values.

Empowering enums with interfaces

Ordinary enums serve as fixed sets of constants. The twist comes when an enum implements an interface, which upgrades the enum from a passive to an active participant in your program. By virtue of the strategy pattern, interfaces bring flexibility to the table. Each enum constant becomes a strategy in itself.

public interface Strategy { void execute(); } public enum Action implements Strategy { ATTACK { @Override public void execute() { /* unleash the Kraken... */ } }, DEFEND { @Override public void execute() { /* raise shields... */ } } }

Singleton behavior fits well here, making your enums both dynamic and thread-safe.

Interfaces add flexibility

Combining enums with interfaces evolves your static elements into complex, behavior-rich objects. They boost the readability and maintainability of your code, significantly enhancing testability. Thanks to a mockable interface, each enum can be swapped for a test double at will.

public interface Reportable { void generateReport(); } public enum Department implements Reportable { SALES { @Override public void generateReport() { /* ... */ } // Sales Report: Selling like hotcakes! }, HR { @Override public void generateReport() { /* ... */ } // HR Report: People come, people go.... } }

The gained flexibility and organizational comfort justify the design choice of enums in combination with interfaces.

Consistency across multiple enums

If you have constants that are closely related and need to embody similar behavior, interfaces allow uniformity. By having multiple enums like SimpleOperators and ComplexOperators, each set of constants can implement, say an Operable interface consistently.

public interface Operable { int operate(int a, int b); } public enum SimpleOperators implements Operable { ADD { public int operate(int a, int b) { return a + b; } // Together we're stronger! }, // ... }

Creating organically extensible code is a significant advantage when using interfaces and enums together in this manner.

Polymorphic enums with interfaces

Leverage the polymorphism of interfaces with enums for fluid and versatile design. It is particularly beneficial in command patterns or state machines, where individual enums behave uniquely but share methods.

Improved code organization

Enums implementing custom interfaces result in a well-organized grouping of constants. Any constants introduced later will adhere to the predefined behavior, fostering maintainable and self-documenting code.