When an enum declares an abstract method, each constant can supply its own implementation in a body attached to that constant, giving each value distinct behavior without a switch.
enum Operation {
ADD { public int apply(int a, int b) { return a + b; } },
MULTIPLY { public int apply(int a, int b) { return a * b; } };
public abstract int apply(int a, int b);
}
System.out.println("ADD: " + Operation.ADD.apply(3, 4));
System.out.println("MULTIPLY: " + Operation.MULTIPLY.apply(3, 4));
ADD: 7
MULTIPLY: 12
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