Scrivio Interview library
LeadershipAdvanced7 min read · updated 12 November 2026

A shared library exposes a sealed interface PaymentEvent with five record subtypes. Consumers write exhaustive switches. A sixth subtype is needed. How do you handle the release?

Adding a sixth permitted subtype to a sealed interface is a breaking change: consumers whose switches are exhaustive without a default stop compiling, and consumer code compiled against the old version throws MatchException at run time when it meets the new subtype. So I would release it as a major version with clear notes and a preparation period, decide and document whether consumers should keep a default branch for events from other teams, and remember that sealed types fit types owned and consumed inside one codebase better than cross-team public APIs.

Why this question is asked

Sealed interfaces with record subtypes are a pleasure to consume: a switch lists every case, the compiler checks that none is missing, and nobody writes a default branch. That pleasure becomes a problem the day the library needs a new case. Interviewers ask this scenario to see whether we understand what exhaustiveness means for compatibility, both when consumers recompile and when they do not, and whether we can plan a release that does not take down other teams' services. In this answer, we will set up the library, see what breaks at compile time and at run time, plan the release, discuss the default branch policy, and reflect on where sealed types belong.

1The Setup: Exhaustive Switches Without Default

The library exposes a sealed interface with five record subtypes, and consumers handle events with switch expressions that cover all five. Because the compiler knows the full list of permitted subtypes, it accepts these switches without a default branch.

Java
// payments-api 3.x
public sealed interface PaymentEvent
permits Authorized, Captured, Refunded, Failed, Expired {}
public record Authorized(String id, BigDecimal amount) implements PaymentEvent {}
// ... Captured, Refunded, Failed, Expired
// A consumer service
String handle(PaymentEvent e) {
return switch (e) {
case Authorized a -> reserve(a);
case Captured c -> ship(c);
case Refunded r -> credit(r);
case Failed f -> alert(f);
case Expired x -> release(x);
}; // exhaustive: no default branch
}

The design is good for consumers precisely because the compiler checks it. The question is what happens when version 4 adds Disputed as a sixth subtype.

2What Breaks When Consumers Recompile

When a consumer upgrades to the new library version and rebuilds, the switch is no longer exhaustive. The compiler reports an error such as the switch expression does not cover all possible input values, and the build fails. This is a source-breaking change in the strict sense.

That failure is actually the feature working as designed: the compiler points to every place that must decide what to do with a disputed payment. For a team that owns both the library and all its consumers, this is the best possible outcome. For fifty consumer teams with their own release schedules, it means fifty builds fail the moment they pick up the new version, which is why the release must be planned.

3What Breaks When They Do Not Recompile

The more dangerous case is consumer code that was compiled against version 3 but runs with version 4 on its classpath, for example because another dependency pulled in the new library, or because producer and consumer communicate through a shared in-process bus. The compiler cannot help here, so javac protects us in a different way: for a switch over a sealed type with no default, it inserts a hidden default branch that throws an exception. Since Java 21 that exception is MatchException.

Step throughNew subtype reaches an old consumer
Producer (v4)KafkaConsumer (v3 code)Old switch
publish Disputed
deliver message
deserialize: unknown type id?
handle(e) if it deserialized
no case matches: hidden default
MatchException
retry or dead-letter

7 messages in order. Play them, or step through them one at a time.

Consumers fail either while deserializing or in the compiler-inserted default branch.

So the first Disputed event that reaches the old consumer code throws MatchException. Across services, the same situation appears in another form: a producer on version 4 publishes a Disputed event to Kafka, and a consumer on version 3 cannot even deserialize it, because its Jackson configuration does not know the type id, and the message fails or goes to a dead-letter topic. Either way, the new subtype must not be produced until consumers are ready for it.

4Planning the Release

I would treat the change as breaking, because it is. First, release it as a new major version, with release notes that name the new subtype, explain what it means in the business, say what a sensible default handling is, and say that the switch will stop compiling. Second, give consumers a preparation period. One useful technique is to publish the new major version early, while producers are still forbidden from emitting Disputed events. Consumers upgrade, fix their switches at their own pace, and deploy. Third, track adoption, for example through dependency reports or a startup log that records the library version each service uses. Only when every consumer that matters runs version 4 do producers switch on the new event, ideally behind a feature flag so it can be turned off quickly.

Step throughRolling out a new sealed subtype
  1. Release v4.0 with Disputedmajor version, notes, migration guidance
  2. Producers keep Disputed switched offfeature flag off in every environment
  3. Consumers upgrade and handle Disputedbuilds fail until the switch is updated
  4. All important consumers on v4?
    YesTurn on Disputed in producersNoWait, follow up with teams
  5. Monitor dead letters and MatchExceptionturn the flag off if anything fails

5 steps in order. Play them, or step through them one at a time.

Consumers move first; the new event appears only after they are ready.

5The Default Branch Policy

The deeper question is whether consumers should be exhaustive at all for events owned by another team. Without a default, every new subtype is a forced change, which is good when a missed case would be a bug, for example money that is never credited. With a default, new subtypes pass through a fallback, which is good when a missed case is harmless, for example a reporting service that can safely ignore unknown events, but the compiler can no longer warn about them.

Java
// A consumer that can safely ignore new events
return switch (e) {
case Captured c -> ship(c);
case Refunded r -> credit(r);
default -> { log.info("ignored {}", e.getClass().getSimpleName()); yield "ignored"; }
};

The library team should document which kind of consumer it expects. For a payments library I would say: services that move money must switch exhaustively and will be told in advance about new subtypes; services that only observe should use a default that logs and continues. Writing that down turns a surprise into an agreed contract.

6Where Sealed Types Belong

This scenario shows the cost of sealing a type that crosses team boundaries: every new variant needs a coordinated release. Sealed types fit best where the producer and all consumers are owned together, inside one service or one team's modules, because there the compiler error is a helpful to-do list rather than a cross-team incident. For events shared across many teams, an alternative design is an open set with an explicit unknown case in the consumer's own model, so new events can be introduced without breaking anyone. The current interface does not have to be undone, but the next shared type should be chosen with this trade-off in mind.

7Follow-up questions

Is adding a permitted subtype a binary-incompatible change?

The old consumer's class files still load and link, so no linkage error appears at startup, which makes the problem easy to miss. But behaviour breaks: the old switch has no case for the new type and throws MatchException when one arrives. Treat it as breaking regardless of how compatibility tools classify it.

Can we add the subtype in a minor version if we document it well?

Only if no consumer relies on exhaustiveness, which we usually cannot know. Semantic versioning tells consumers to expect breaks in a major version; putting one in a minor version breaks automated upgrades that trust it.

What did older Java versions throw?

Before Java 21, an exhaustive enum switch expression without default threw IncompatibleClassChangeError when it met an unknown constant. Since Java 21 (JEP 441), exhaustive switches over enums and sealed types throw MatchException instead. Check the release your consumers compile for.

8Conclusion

In this answer, we saw that adding a sixth subtype to a sealed PaymentEvent breaks consumers in two ways: exhaustive switches without default stop compiling, and code compiled against the old version throws MatchException when the new subtype arrives, or fails earlier during deserialization. We released it as a major version with notes, let consumers upgrade first while producers kept the new event behind a flag, documented a default-branch policy for observing consumers, and noted that sealed types suit types owned within one codebase better than cross-team APIs.

What to say in the interviewAbout 100 seconds aloud

Open with

I would start by being clear that adding a sixth permitted subtype is a breaking change, even though it looks additive. Consumers who switch exhaustively over PaymentEvent without a default will get a compile error when they upgrade, because the switch no longer covers all cases.

Then, if they want more

Consumers who do not recompile are worse off: javac inserts a hidden default into exhaustive pattern switches that throws MatchException, so the first new event that reaches old code throws at run time. Across services, an old consumer may not even be able to deserialize the new event type.

So I would release it as a major version, with release notes explaining the new subtype, what it means for the business and how to handle it.

The order matters: consumers upgrade first while producers keep the new event switched off behind a feature flag, then once the consumers that matter are on the new version, producers turn it on, and we watch dead-letter topics and errors. I would also document a policy for default branches.

Services that move money should switch exhaustively, because a missed case is a bug they want the compiler to find. Services that only observe events should have a default that logs and continues.

And as a lesson, I would point out that sealed types are best for types produced and consumed inside one codebase, where the compiler error is a useful to-do list. For widely shared cross-team events, an open model with an explicit unknown case is often the better choice.

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.
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 "A shared library exposes a sealed interface PaymentEvent with five record subtypes....": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/79abc5d2b0/