Quick revision
The short answer
For a switch over a sealed type, the compiler reads the permits list and checks that every permitted subtype is covered by some unguarded case, recursing into subtypes that are sealed themselves and into record components for record patterns. When the switch is exhaustive, no default is needed, but javac still adds a hidden default that throws. If a new permitted subclass appears at run time without the switch being recompiled, that hidden default throws MatchException.
What to remember
In this answer, we saw how the compiler checks exhaustiveness for sealed types: it collects the unguarded cases, treats a sealed type as covered when all permitted subtypes are covered, recursing through sealed subtypes and record components, and accounts for type arguments in generic hierarchies. It then inserts a synthetic default that throws MatchException, which is what fires when a separately compiled subtype appears. Pattern switch statements are checked too, while old constant-only statements are not.
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.
For a switch over a sealed type, the compiler reads the permits list and checks that every permitted subtype is covered by some unguarded case, recursing into subtypes that are sealed themselves and into record components for record patterns. When the switch is exhaustive, no default is needed, but javac still adds a hidden default that throws. If a new permitted subclass appears at run time without the switch being recompiled, that hidden default throws MatchException.
Why this question is asked
Exhaustiveness checking is the feature that makes sealed types worth using: add a variant, and the compiler lists every place that needs to handle it. Interviewers ask how it works because the details explain both its strength and its limits, in particular what happens when code is compiled separately, which is normal in any system with libraries and plugins. In this answer, we will recap sealed types and exhaustive switches, walk through how the compiler checks coverage, see what it inserts into the bytecode, reproduce the run-time failure, and finish with switch statements and generic hierarchies.
1A Quick Recap of Sealed Types and Exhaustive Switches
A sealed interface or class lists the only types allowed to extend it, for example sealed interface Shape permits Circle, Square, Triangle. Each permitted type must be final, sealed again, or non-sealed, which reopens that branch to any subclass.
A switch is exhaustive when every possible value of the selector is handled by some case. Switch expressions, the kind that produce a value, have always had to be exhaustive. Before sealed types, the only way to be sure was a default branch. With sealed types, the compiler can know the complete list of subtypes, so a switch that names them all is exhaustive without a default.
2How the Compiler Checks Coverage
The compiler builds up the set of types the cases cover and asks whether that set covers the selector type. A sealed type counts as covered when all of its permitted subtypes are covered. That rule is applied recursively, so if Shape permits Polygon and Circle, and Polygon is itself sealed with Square and Triangle, then cases for Circle, Square and Triangle together cover Shape.
- List the unguarded casesguards are ignored for coverage
- Is the selector type covered directly?YesExhaustiveNoLook at its permitted subtypes
- Is every permitted subtype covered?YesExhaustiveNoRecurse into sealed subtypes
- Still a gap after recursion?YesCompile error: not all values coveredNoExhaustive
- Insert a synthetic default that throwsfor values unknown at compile time
5 steps in order. Play them, or step through them one at a time.
Record patterns repeat the same check for each component.
Guarded cases do not count toward coverage, because the compiler does not reason about the condition. Record patterns are checked component by component: if Pair(Shape a, Shape b) is a record, the cases Pair(Circle c, var b) and Pair(Polygon p, var b) cover it because the first component is fully covered and the second uses var. Remove the Polygon case and the compiler reports that the switch expression does not cover all possible input values.
3The Hidden Default Javac Inserts
Exhaustive at compile time does not mean exhaustive forever. The switch was checked against the permits list as it was when the switch was compiled, but the sealed hierarchy might live in another library that is upgraded later. To make that case fail loudly rather than fall through, javac adds a synthetic default branch to every exhaustive pattern switch without an explicit one. That branch throws MatchException.
There is some history here. Exhaustive switch expressions over enums existed before Java 21, and their synthetic default threw IncompatibleClassChangeError. Pattern switches use MatchException. Which of the two a plain enum switch expression throws today depends on the compiler, so if the exact exception matters to your code, check your Java version.
4Reproducing the Run-Time Failure
Suppose the shapes live in shapes.jar, compiled with Circle and Polygon, and our area method is compiled against it. A new version of shapes.jar adds Hexagon to Polygon's permits list, and we drop it onto the classpath without recompiling our code.
Notice what does not happen: the switch does not silently return a wrong value or skip the case. The hidden default turns the gap into an immediate, clearly named failure. If we recompile our code against v2, javac refuses it until we add a Hexagon case, which is exactly the help we wanted. The lesson for a build is to compile modules that share a sealed hierarchy together, or at least to rebuild consumers when the hierarchy's owner releases.
5Switch Statements and Generic Hierarchies
Since Java 21, switch statements are also checked when they use patterns or case null. A pattern switch statement over Shape that forgets Triangle fails to compile, just like the expression. Old-style switch statements over enums, strings or ints, with only constant labels, are not checked, for compatibility with decades of existing code; a missing enum constant there simply does nothing.
Generic sealed hierarchies are checked with their type arguments in mind. In the example below, A can only ever be an I<String>, so a switch over I<Integer> does not need a case for A at all.
The compiler works out that A is impossible for this selector type and leaves it out of the coverage check, so the switch is exhaustive with one case.
6Follow-up questions
Does a non-sealed subtype break exhaustiveness?
No. A non-sealed permitted subtype can have any subclasses, but a case for the non-sealed type itself covers all of them, so the switch can still be exhaustive. What we cannot do is list its subclasses individually and expect coverage.
Should we add a default anyway, just in case?
Usually not inside the module that owns the hierarchy. A default removes the compile error when a new subtype is added, so we lose the check that finds every place to update. The synthetic default already protects us at run time.
Are enums treated the same way?
Yes for coverage. An enum is a closed set, so a switch expression or a pattern switch statement listing every constant is exhaustive, and enum constants can now be mixed with sealed types using qualified names such as case Color.RED.
7Conclusion
In this answer, we saw how the compiler checks exhaustiveness for sealed types: it collects the unguarded cases, treats a sealed type as covered when all permitted subtypes are covered, recursing through sealed subtypes and record components, and accounts for type arguments in generic hierarchies. It then inserts a synthetic default that throws MatchException, which is what fires when a separately compiled subtype appears. Pattern switch statements are checked too, while old constant-only statements are not.
Open with
For a switch over a sealed type, the compiler takes the unguarded cases and checks that they cover the selector type. A sealed type is covered when every type in its permits list is covered, and that is applied recursively, so if a permitted subtype is itself sealed, covering its subtypes counts.
Then, if they want more
For record patterns it does the same check per component. Guarded cases do not count, because it does not reason about conditions. If coverage is complete, no default is needed, but javac still inserts a synthetic default branch that throws.
That matters for separate compilation: if a library adds a new permitted subclass and my switch is not recompiled, a value of that new type reaches the synthetic default and I get a MatchException at run time instead of silent wrong behaviour. Recompiling then turns it into a compile error that points at the switch.
Older enum switch expressions historically threw IncompatibleClassChangeError there instead, so I would check the version if that matters. Since Java 21, switch statements that use patterns or case null must also be exhaustive, while old constant-only statement switches over enums or strings are not checked.
And for generic sealed hierarchies the check considers type arguments, so a subtype that can only be I of String does not need a case in a switch over I of Integer.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- 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 "How does the compiler check exhaustiveness for switches over sealed types, and what...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/82cf8bc761/