Quick revision
The short answer
Sealing a public type means every new variant is a breaking change for consumers that switch exhaustively. Inside one module or one team's services that coupling is useful, because the compiler finds every place to update; across team or company boundaries it becomes a coordination cost. So my organization-wide rule would be: sealed types freely inside a module or team, open types or tolerant parsing with an explicit unknown case across boundaries, and sealed types in shared libraries only where the domain itself fixes the set, such as a success-or-failure result. The rule goes into the API review guidelines and is checked in library reviews.
What to remember
In this answer, we saw that sealing makes the set of variants part of the public contract, so every new variant breaks consumers that switch exhaustively. Inside a module or one team's services, that coupling is useful because the compiler finds every place to update. Across team or company boundaries, it becomes coordination, so we prefer open types and tolerant parsing with an explicit Unknown case. Shared libraries may use sealed types only where the domain fixes the set, and the rule lives in API review guidelines, library reviews and build checks.
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.
Sealing a public type means every new variant is a breaking change for consumers that switch exhaustively. Inside one module or one team's services that coupling is useful, because the compiler finds every place to update; across team or company boundaries it becomes a coordination cost. So my organization-wide rule would be: sealed types freely inside a module or team, open types or tolerant parsing with an explicit unknown case across boundaries, and sealed types in shared libraries only where the domain itself fixes the set, such as a success-or-failure result. The rule goes into the API review guidelines and is checked in library reviews.
Why this question is asked
Sealed types are one of the best tools Java has added for modelling a domain, and the natural instinct is to use them everywhere. The organization learns the cost later, when a shared library cannot add a variant without fifty teams changing code at the same time. Interviewers ask this question to see whether we can see coupling as a property of an API, decide where it helps and where it hurts, and turn that into a rule that people can apply without a long debate each time. In this answer, we will look at what sealing promises to consumers, compare the inside and outside of a boundary, look at the tolerant alternative, write the rule, and see how to enforce it.
1What Sealing Promises to Consumers
A sealed interface lists all its direct subtypes. Consumers can therefore write a switch that handles every subtype with no default, and the compiler checks it. That is a promise from the producer: these are all the cases there will ever be in this version. When a variant is added, the promise changes, and every exhaustive switch in every consumer either fails to compile or, if not recompiled, throws MatchException when the new variant appears.
So sealing turns the set of variants into part of the public contract, as binding as a method signature. That is neither good nor bad on its own. It is coupling, and the question for an organization is where coupling of this kind is worth having.
2Inside a Boundary, Coupling Is a Tool
Within one module, or among services that one team owns and releases together, adding a variant is a change the same people make in one pull request. The compiler errors that follow are a precise list of every place that must handle the new case: the renderer, the persistence mapping, the metrics, the API translation. Nothing is forgotten, and no runtime surprise is possible because everything is compiled together.
This is the best possible use of sealed types. The cost of adding a variant is real but small, it is paid by the people who wanted the change, and the compiler does the searching.
3Across a Boundary, Coupling Becomes Coordination
Across team or company boundaries, the same change hits people who did not ask for it, on their schedule, not ours. Each consumer's build breaks when it upgrades, and producers must wait until every consumer is ready before emitting the new variant. Multiply that by many shared types and many teams, and the shared library becomes a place where nobody dares to add anything, which pushes teams into workarounds such as overloading an existing variant with a new meaning.
| Where the type is used | Rule | Why |
|---|---|---|
| Inside one module | Sealed freely | Compiler finds every place to update |
| Services of one team, released together | Sealed allowed | Same people pay and benefit |
| Shared library, set fixed by the domain | Sealed allowed after review | e.g. Success / Failure will not grow |
| Shared library, set may grow | Open type or Unknown case | Avoid cross-team forced changes |
| Public or partner API | Tolerant parsing, Unknown case | Consumers upgrade on their own schedule |
The alternative across boundaries is to design for variants the consumer does not know yet. In the consumer's own model, that means an explicit unknown case; on the wire, it means tolerant parsing, where an unrecognised type is mapped to that case rather than rejected. Note that the unknown case lives in the consumer: the consumer can still keep its own internal model sealed, with Unknown as one of its variants, and switch exhaustively over that.
4A Tolerant Consumer Model
Here is a consumer that reads payment events from another team. It maps the wire format into its own sealed model with an explicit Unknown variant, so it stays exhaustive internally while tolerating new types from outside.
The producer can now add a disputed event without breaking this consumer. The consumer decides deliberately what Unknown means for it, perhaps log and skip, perhaps park for review, and it can add a Disputed case later on its own schedule.
5Writing the Rule and Checking It
The rule I would put in the API review guidelines has three parts. Sealed types may be used freely inside a module and among the services of one team that are released together. In shared libraries and public APIs, a sealed type requires a short justification that the set of variants is fixed by the domain, such as Result with Success and Failure, or Optional-like presence and absence, and will not grow as the business grows. Everywhere else across boundaries, use open types, and consumers model unknown variants explicitly and parse tolerantly.
Rules only work if they are checked. Library reviews should include a question about every sealed type in the public API, and an ArchUnit test or a simple build check can list sealed types in exported packages so none is added without review. Release notes should flag any change to a permits list as breaking, and compatibility tools such as japicmp can be configured to fail the build when a public permits list changes outside a major version; check the tool's version for support of sealed types.
6Follow-up questions
Isn't an Unknown case just a default branch with extra steps?
It is more explicit. A default branch hides unknown values inside each switch, while an Unknown variant makes them a visible part of the model, so every consumer decides once, in one place, what to do with them, and that decision is easy to test and to monitor.
What if a team insists their shared type really is fixed?
Ask what would happen if the business added a variant next year. If the honest answer is that the type would need a new variant, it is not fixed. If adding a variant would mean a different concept altogether, like adding a third value to a boolean result, sealing is reasonable.
Do protocol formats like Protobuf have the same issue?
Yes. Protobuf enums and oneofs face the same question, and the usual guidance is that readers must handle unknown values, which the generated code exposes. The organizational rule is the same: closed sets inside, tolerance across boundaries.
7Conclusion
In this answer, we saw that sealing makes the set of variants part of the public contract, so every new variant breaks consumers that switch exhaustively. Inside a module or one team's services, that coupling is useful because the compiler finds every place to update. Across team or company boundaries, it becomes coordination, so we prefer open types and tolerant parsing with an explicit Unknown case. Shared libraries may use sealed types only where the domain fixes the set, and the rule lives in API review guidelines, library reviews and build checks.
Open with
I would start from what sealing promises. A sealed public type tells consumers these are all the variants, so they can switch exhaustively without a default. That means the list of variants becomes part of the contract, and adding one is a breaking change: consumers' builds fail, and code that is not recompiled throws MatchException.
Then, if they want more
Whether that is good depends on who pays. Inside one module, or across services that one team owns and releases together, it is excellent: the person adding a variant gets a compiler error at every place that must handle it, in the same pull request.
Across team or company boundaries, it becomes coordination, because other teams must change code on our schedule, and producers cannot emit the new variant until every consumer is ready. So my rule would be: sealed types freely inside a module and within a team.
In shared libraries and public APIs, sealed types only with a review that shows the set is fixed by the domain, like a Result with success and failure, which will not grow with the business. Otherwise, use open types, and consumers parse tolerantly into their own model with an explicit Unknown variant, which they can still seal internally.
I would write that into the API review guidelines, add a question about sealed types to every library review, list sealed types in exported packages with an ArchUnit check, and treat any change to a public permits list as a major-version change.
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.
- 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 "Sealed hierarchies give compile-time guarantees inside one codebase but become...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/6ae7b971bd/