Quick revision
The short answer
Choose a sealed interface over an enum when the variants carry different data or need many instances each; over an open interface when the domain fixes the set of variants and we want the compiler to check exhaustive switches; and over an abstract class with a private constructor, the pre-Java 17 trick, because the compiler can use a sealed hierarchy for exhaustiveness and the trick gave it nothing. non-sealed lets us open one branch while keeping the rest closed. Sealed types fit results, commands and events that one module owns.
What to remember
In this answer, we compared four ways to model a closed set of kinds. A sealed interface beats an enum when variants carry different data or need many instances, beats an open interface when the domain fixes the set and we want exhaustive switches, and beats the old private-constructor abstract class because the compiler can use it for exhaustiveness. non-sealed opens one branch while keeping the rest closed, and sealed types pay off most for results, commands and events owned by one module.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Choose a sealed interface over an enum when the variants carry different data or need many instances each; over an open interface when the domain fixes the set of variants and we want the compiler to check exhaustive switches; and over an abstract class with a private constructor, the pre-Java 17 trick, because the compiler can use a sealed hierarchy for exhaustiveness and the trick gave it nothing. non-sealed lets us open one branch while keeping the rest closed. Sealed types fit results, commands and events that one module owns.
Why this question is asked
Java now has four ways to model a type with several kinds of values, and choosing between them is a design decision that shows up in every switch statement for years afterwards. Interviewers ask this question to see whether we can explain the trade-offs rather than just state a preference. In this answer, we will recap what each option guarantees, compare a sealed interface with an enum, with an open interface and with the private-constructor pattern, look at non-sealed for partial openness, and finish with the kinds of types where sealing pays off.
1The Four Options in Brief
An enum is a fixed set of named singleton instances: there is exactly one PENDING and one PAID in the whole program. An open interface allows any class, anywhere, to implement it. An abstract class with only private constructors can be extended only by its own nested classes, which was the old way to simulate a closed hierarchy. A sealed interface or class lists its permitted direct subtypes, and the compiler and JVM enforce that list.
| Option | Variants carry data? | Set closed? | Exhaustive switch? |
|---|---|---|---|
| enum | Same fields for all, one instance each | Yes | Yes |
| open interface | Yes, any shape | No | No, needs default |
| abstract class, private constructor | Yes | Yes, by convention | No |
| sealed interface + records | Yes, each its own shape | Yes, enforced | Yes |
2Sealed Interface Versus Enum
An enum is perfect when the variants are simple labels, perhaps with a few fixed attributes: order statuses, days of the week, log levels. It breaks down when each variant needs different data, or many different instances. Consider payment methods: a card payment has a masked number and an expiry, a bank transfer has an IBAN, a wallet payment has a provider and a token. An enum can only give every constant the same fields, and each constant is a single instance, so it cannot hold the card details of thousands of customers.
So the sealed interface keeps what we liked about the enum, a closed set the compiler checks, and gives each variant its own shape. If we still want the label, for example to store in a database column, the enum and the sealed type can live side by side.
3Sealed Interface Versus Open Interface
An open interface is right when we expect others to add implementations we cannot foresee: a Spring HandlerInterceptor, a pricing rule plug-in, a storage driver. Callers then use polymorphism, calling the interface's methods, and never need to know the concrete type.
When the set of variants is fixed by the domain, and code needs to handle each variant differently, sealing is better. With an open interface, a switch over the types needs a default branch, and when someone adds a new implementation, the default silently swallows it. With a sealed interface, the compiler knows the full list, accepts a switch without default, and reports an error in every switch that misses a new variant. The trade is flexibility for safety: nobody outside the module can add a variant, and every variant added inside is found by the compiler.
4Why the Private-Constructor Trick Was Not Enough
Before Java 17, the way to close a hierarchy was an abstract class whose only constructor is private, with the subclasses nested inside, so only they can call it.
The hierarchy is closed in practice, but the compiler does not know it. It sees an ordinary abstract class and cannot prove that Circle and Square are the only subclasses, so it cannot check exhaustiveness and we must write a fallback branch. It also forces all subclasses to be nested in one file and works only for classes, not interfaces. A sealed type states the restriction directly, works for interfaces and for subclasses in separate files, and gives the compiler the information it needs.
5Opening One Branch With non-sealed
Sometimes most of a hierarchy is fixed but one branch must allow extension. Suppose notifications are email, SMS, or a custom channel that other teams in the same codebase can provide. We can seal the top and mark one branch non-sealed.
The switch is still exhaustive, because every possible Notification is an Email, an Sms or some CustomChannel. The extension point is explicit and contained: inside that branch, implementers use polymorphism through deliver(), while the fixed variants keep compiler-checked handling.
6Where Sealed Types Pay Off
Sealed types work best for types that one module owns and whose variants are part of its meaning. Results are a classic example: Success, NotFound, Rejected, each with its own data, returned by a service and handled by its callers. Commands and events within a bounded context, the part of the system one team owns, are another: PlaceOrder, CancelOrder, OrderPlaced, OrderCancelled. Abstract syntax trees, state machines and protocol messages are others. Across team or company boundaries, sealing becomes a coordination cost, because adding a variant breaks every consumer's exhaustive switch, so there an open type or an explicit unknown variant is often wiser.
7Follow-up questions
Can an enum implement a sealed interface?
Yes. An enum is implicitly final, so it can be a permitted subtype. This is useful when some variants carry data and others are simple singletons, for example a sealed Shape with records for Circle and Square and an enum constant for a predefined unit shape.
Do we lose polymorphism by switching over sealed types?
We gain a choice. Behaviour that belongs to every variant can still be an interface method each record implements. Switching is better when the operation belongs to the caller, such as rendering or persistence, so it should not be added to the domain types themselves.
Is a sealed abstract class ever better than a sealed interface?
When the variants share state or implementation, a sealed abstract class can hold it. But records cannot extend classes, so if the variants are records, the parent must be an interface, which is why sealed interfaces are more common.
8Conclusion
In this answer, we compared four ways to model a closed set of kinds. A sealed interface beats an enum when variants carry different data or need many instances, beats an open interface when the domain fixes the set and we want exhaustive switches, and beats the old private-constructor abstract class because the compiler can use it for exhaustiveness. non-sealed opens one branch while keeping the rest closed, and sealed types pay off most for results, commands and events owned by one module.
Open with
I think about it by asking two questions: do the variants carry different data, and is the set fixed by the domain. If the variants are just labels with the same few attributes, like order status, an enum is simplest.
Then, if they want more
Once each variant needs its own data or there are many instances, like a card payment with a masked number and a bank transfer with an IBAN, an enum cannot do it, and a sealed interface with record variants keeps the closed set but gives each variant its own shape.
Against an open interface, the question is whether others should add implementations. For plug-ins and strategies, open is right and callers use polymorphism.
When the domain fixes the variants and code handles each one differently, sealing lets the compiler accept a switch without default and flag every switch when a variant is added, while an open interface needs a default that silently swallows new types.
Before Java 17 we closed hierarchies with an abstract class and a private constructor, but the compiler did not understand that, so we still needed fallback branches and could not use interfaces. If one branch should be extensible, I mark it non-sealed, and switches stay exhaustive because that branch is covered as a whole.
Sealed types fit best for results, commands and events that one module owns. Across team boundaries I am more careful, because adding a variant becomes a breaking change for consumers.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "When would you choose a sealed interface over an enum, over an open interface, or over...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/61d45af9df/