An enum can implement an interface and give each constant its own override, so calling code invokes behavior polymorphically instead of switching on the enum value.
interface Discount {
double apply(double price);
}
enum Tier implements Discount {
BRONZE { public double apply(double price) { return price; } },
GOLD { public double apply(double price) { return price * 0.9; } };
}
for (Tier t : Tier.values()) {
System.out.println(t + ": " + t.apply(100.0));
}
BRONZE: 100.0
GOLD: 90.0
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27