Core Java: An enum constant can override an abstract method with its own body

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.

Code
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));
Output
ADD: 7
MULTIPLY: 12
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