Core Java: An enum implementing an interface swaps a switch for polymorphism

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.

Code
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));
}
Output
BRONZE: 100.0
GOLD: 90.0
Advertisement
More in JAVA

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

© Java Coding Hub · About · Contact · Privacy · Terms