Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Write an enum Operation with PLUS and TIMES, each holding a symbol and implementing apply(int a, int b), in two ways: constant-specific bodies and a lambda field.

With constant-specific bodies, the enum declares abstract int apply(int a, int b) and each constant overrides it in its own body, which makes each constant an anonymous subclass. With a lambda field, each constant passes its behaviour to the constructor as an IntBinaryOperator, and a single apply() method delegates to it. In both versions the symbol and the behaviour are final fields, and the enum can implement an interface so callers depend on the behaviour, not on the enum.

Why this question is asked

Giving each enum constant its own behaviour is a classic interview exercise because it tests several things at once: enum constructors and fields, abstract methods, anonymous classes, functional interfaces and API design. It also has a practical use, replacing a switch over operation types that has to be updated in several places. In this answer, we will write the Operation enum with constant-specific bodies, then with a lambda field, look at what the compiler generates for each, and finally put an interface in front of it.

1A Quick Recap: Enums Can Have State and Behaviour

An enum constant is an object, so the enum can declare fields, a constructor and methods. Each constant can pass arguments to the constructor in parentheses, like PLUS("+"). The constructor is always private, and since every constant is shared by the whole program, its fields should be final so nobody can change a constant's state.

There are two ways to give each constant different behaviour. The enum can declare an abstract method that each constant implements in its own body, or it can store a function in a field. Both are correct; they differ in readability and in what they generate.

2Version 1: Constant-Specific Bodies

Here the enum declares an abstract method, and each constant provides a body that implements it. The compiler refuses to compile the enum if any constant forgets the method, which is a useful guarantee.

Java
public enum Operation {
PLUS("+") {
@Override public int apply(int a, int b) { return a + b; }
},
TIMES("*") {
@Override public int apply(int a, int b) { return a * b; }
};
private final String symbol;
Operation(String symbol) { this.symbol = symbol; }
public String symbol() { return symbol; }
public abstract int apply(int a, int b);
}
System.out.println(Operation.PLUS.apply(3, 4)); // 7
System.out.println(Operation.TIMES.apply(3, 4)); // 12
System.out.println(Operation.TIMES.symbol()); // *

Notice the semicolon after the last constant. It ends the list of constants, and it is required whenever the enum has anything after the constants, such as fields or methods. Each constant body can also contain its own private helper methods, which is handy when the behaviour is more than one line.

3What the Bodies Generate

A constant with a body is not an instance of Operation itself. The compiler generates an anonymous subclass for each such constant, Operation$1 and Operation$2, and creates the constant as an instance of that subclass. That has a visible consequence when we ask for the class.

Java
System.out.println(Operation.PLUS.getClass()); // class Operation$1
System.out.println(Operation.PLUS.getDeclaringClass()); // class Operation
System.out.println(Operation.PLUS.getClass() == Operation.class); // false
Class versus declaring class
ExpressionNo bodyWith a body
getClass()OperationOperation$1 (anonymous subclass)
getDeclaringClass()OperationOperation
Enum class isfinalsealed to its constant bodies

So code that needs the enum type of a constant must use getDeclaringClass(), not getClass(). EnumSet, EnumMap and Enum.valueOf() already do this. It also explains why an enum with constant bodies is not final: it is implicitly sealed, permitting only its own constant subclasses.

4Version 2: A Lambda Field

The second version keeps the behaviour as data. Each constant passes a lambda to the constructor, the enum stores it in a final field of type IntBinaryOperator, a functional interface from java.util.function that takes two ints and returns an int, and one concrete apply() method delegates to it.

Java
import java.util.function.IntBinaryOperator;
public enum Operation {
PLUS("+", (a, b) -> a + b),
TIMES("*", (a, b) -> a * b);
private final String symbol;
private final IntBinaryOperator op;
Operation(String symbol, IntBinaryOperator op) {
this.symbol = symbol;
this.op = op;
}
public String symbol() { return symbol; }
public int apply(int a, int b) { return op.applyAsInt(a, b); }
}
System.out.println(Operation.PLUS.apply(3, 4)); // 7
System.out.println(Operation.PLUS.getClass()); // class Operation: no subclass
System.out.println(Operation.TIMES.apply(6, 7)); // 42

This version is shorter and keeps each constant on one line, so the whole table of operations is easy to read. IntBinaryOperator works on primitive ints, so there is no boxing. The trade-off is that a long or complex behaviour becomes an awkward lambda inside the constant list; for those, constant bodies or a separate method reference read better.

5Putting an Interface in Front

Enums can implement interfaces. If callers depend on an interface rather than on the enum, they can accept other implementations too, for example operations defined in a plugin, without the enum ever changing. This is the extensible enum pattern.

Java
public interface BinaryOp {
String symbol();
int apply(int a, int b);
}
public enum Operation implements BinaryOp {
PLUS("+", (a, b) -> a + b),
TIMES("*", (a, b) -> a * b);
// fields, constructor, symbol() and apply() as in version 2
}
public enum ExtraOperation implements BinaryOp {
MINUS("-", (a, b) -> a - b);
// same shape
}
int evaluate(BinaryOp op, int a, int b) { return op.apply(a, b); }
evaluate(Operation.PLUS, 2, 3); // 5
evaluate(ExtraOperation.MINUS, 2, 3); // -1

Notice that evaluate() never mentions Operation. The enum is still a convenient, closed set for the standard operations, while the interface keeps the calling code open. The fields stay final in every version, because each constant is a shared singleton, and a mutable field on a constant would be global mutable state.

6Follow-up questions

Which version would you choose?

For one-line behaviours, the lambda field, because the constant list reads like a table and no subclasses are generated. For longer behaviour, or when a constant needs helper methods, constant-specific bodies, because a lambda spanning many lines inside the constant list is hard to read. Both give the compile-time guarantee that every constant has a behaviour, the lambda version through the constructor's parameter.

Can a constant body add a public method that callers use?

It can declare one, but callers cannot reach it. The static type of Operation.PLUS is Operation, so only methods declared in the enum itself are visible; a method that exists only in PLUS's anonymous subclass can be called from inside that body and nowhere else. If callers need a method, declare it in the enum, abstract or with a default body.

Why not just use a switch on the constant inside apply()?

A switch works, but behaviour and constant then live in different places, and adding a constant means remembering to update the switch. With constant bodies or lambda fields, the behaviour is part of the constant's declaration, so it cannot be forgotten.

7Conclusion

In this answer, we wrote the Operation enum two ways. With constant-specific bodies, the enum declares an abstract apply() and each constant overrides it, which generates an anonymous subclass per constant, so getClass() differs from getDeclaringClass(). With a lambda field, each constant passes an IntBinaryOperator to the constructor and one apply() delegates to it. In both, fields are final because constants are shared singletons, and implementing an interface lets callers depend on the behaviour rather than the enum.

What to say in the interviewAbout 80 seconds aloud

Open with

I can write it two ways. The first uses constant-specific bodies: the enum has a private final String symbol set by the constructor, so PLUS passes plus and TIMES passes star, and it declares public abstract int apply of two ints.

Then, if they want more

Then each constant has its own body that overrides apply, PLUS returning a plus b and TIMES returning a times b. The compiler makes sure every constant implements it.

The catch is that each constant with a body becomes an anonymous subclass, so PLUS.getClass() is Operation dollar one, while getDeclaringClass() is Operation, which is why EnumMap and valueOf use the declaring class.

The second way stores behaviour as data: each constant passes a lambda to the constructor, like PLUS with plus and a comma b arrow a plus b, the enum keeps it in a final IntBinaryOperator field, and a single apply method calls applyAsInt. No subclasses, no boxing, and the constants read like a table.

I prefer lambdas for one-liners and bodies for longer logic. In both cases fields are final, because constants are shared singletons. And I would have the enum implement a BinaryOp interface, so callers depend on the interface and other enums or classes can add operations without changing this one.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write an enum Operation with PLUS and TIMES, each holding a symbol and implementing...": https://scrivio-interview-library.netlify.app/java/enums/5b6582687a/