Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

A candidate says that Java code using records, sealed interfaces and pattern matching is not object-oriented. How would you respond, and how do the four pillars apply to such a design?

I would say the claim confuses one style of object orientation with the whole idea. Records still encapsulate data, immutably and with validation in compact constructors; a sealed interface is an abstraction with a known, closed set of implementations; a switch with pattern matching is polymorphism decided by the caller instead of by virtual dispatch; and inheritance is limited on purpose in favour of composition. Data-oriented and classic object-oriented styles suit different problems, closed sets of variants with many operations versus open sets of types with few operations, and good Java code uses both.

Why this question is asked

Since Java 21, records, sealed interfaces and pattern matching in switch are all standard, and a lot of new code uses them to model data. Some engineers see this as a step away from object orientation, and this interview question tests whether we can discuss that calmly and precisely instead of defending a camp. It also checks that we know when each style fits, which is a real design decision in every service. In this answer, we will look at what the claim gets right, go through the four pillars in a record-based design, compare the two styles using the problem each one solves, and show how they combine in one codebase.

1What the Claim Gets Right

Classic object-oriented design puts behaviour inside each class. To compute a shipping cost, we call parcel.cost() and each parcel type answers for itself through virtual dispatch, meaning the JVM picks the method from the object's real class at run time. Data-oriented design, by contrast, keeps types as plain data and writes operations as functions over them, often a switch over the possible types.

So the claim notices something real: code like the example below looks more like a functional language than like a textbook class hierarchy. But looking different is not the same as abandoning the ideas. Let us check each pillar.

Java
sealed interface Parcel permits Letter, Box, Pallet {}
record Letter(int grams) implements Parcel {}
record Box(int grams, Dimensions size) implements Parcel {}
record Pallet(int kilos) implements Parcel {}
static Money shippingCost(Parcel p) {
return switch (p) {
case Letter l when l.grams() <= 20 -> Money.of("0.95");
case Letter l -> Money.of("1.60");
case Box b -> Money.of("5.00").plus(perKilo(b.grams()));
case Pallet pl -> Money.of("80.00").times(pl.kilos() / 100 + 1);
}; // exhaustive: no default needed, the compiler knows all permitted types
}

2Encapsulation Still Holds in Records

Encapsulation means an object protects its own rules. A record's fields are private and final, and its compact constructor, a constructor without a parameter list that runs before the fields are assigned, can validate and normalise every value. Since the record is immutable, once it exists it can never become invalid.

Java
record Box(int grams, Dimensions size) implements Parcel {
Box {
if (grams <= 0) throw new IllegalArgumentException("grams must be positive");
Objects.requireNonNull(size);
}
}
new Box(1200, new Dimensions(30, 20, 10)); // ok
new Box(0, new Dimensions(30, 20, 10)); // IllegalArgumentException: grams must be positive

In one respect, records encapsulate more strongly than typical mutable classes with setters: there is no way to change a field after construction. What records give up is hiding their representation, since accessors expose every component. That is a deliberate choice for types whose data is their meaning.

3Abstraction and Polymorphism, Decided by the Caller

Parcel is an abstraction: a type that callers use without knowing which concrete parcel they have. The difference from classic design is that the set of implementations is closed. The permits clause lists them, and nobody outside can add another.

The switch is polymorphism too. One call, shippingCost(p), does different things for different types. The only difference is who chooses: in virtual dispatch, each class supplies its method and the JVM picks it; in pattern matching, the caller lists the cases and the compiler checks that none is missing. Both give us behaviour that varies by type without if-else chains on type codes.

Two ways to vary behaviour by type
QuestionVirtual dispatchSealed type + switch
Where behaviour livesIn each classIn the operation, by case
Adding a new typeEasy: one new classEdit every switch (compiler lists them)
Adding a new operationEdit every classEasy: one new function
Who can add typesAnyoneOnly the permitted list
Good fitPlug-ins, strategies, open setsEvents, commands, results, ASTs
This is the classic expression problem: each style makes one axis of change cheap.

4Inheritance, Limited on Purpose

Records cannot extend classes, and the sealed interface limits who may implement it. That restricts inheritance deliberately. The reason is the long experience that deep implementation hierarchies couple subclasses to their parents' internals. Data-oriented code shares behaviour through composition, for example a Box holding a Dimensions value, and through small interfaces, not through a chain of abstract base classes. Most modern object-oriented guidance already says to prefer composition, so this is a continuation of good practice, not a break from it.

5Two Styles for Two Kinds of Problem

The useful way to answer the candidate is to say when each style fits. If the set of variants is closed and stable but the operations keep growing, data-oriented design wins: payment events, API responses, parsed expressions. Adding a new report or rule is one new function, and the compiler tells us if a case is forgotten.

If the set of types is open, with new ones added by other teams or plug-ins, and the operations are few and stable, classic polymorphism wins: notification channels, storage backends, pricing strategies. Adding a type is a new class, and nobody edits a central switch.

6Mixing Both in One Codebase

Good Java code chooses per model, and the two styles can even meet in one type. A sealed interface can declare an abstract method that every record implements, while other operations use switches. We pick the core behaviour that truly belongs to the type and keep reporting and formatting logic outside.

Java
sealed interface Parcel permits Letter, Box, Pallet {
int weightGrams(); // classic: each type answers for itself
}
record Letter(int grams) implements Parcel { public int weightGrams() { return grams; } }
// Box implements weightGrams() the same way, returning its grams
record Pallet(int kilos) implements Parcel { public int weightGrams() { return kilos * 1000; } }
int total = parcels.stream().mapToInt(Parcel::weightGrams).sum(); // virtual dispatch
String label = switch (parcel) { case Letter l -> "L"; case Box b -> "B"; case Pallet p -> "P"; };

So my answer to the candidate would be that this is object-oriented design with a different default for where behaviour lives. The pillars are all present, and choosing the style per model is a sign of design maturity, not a departure from it.

7Follow-up questions

Is a switch over types not exactly the instanceof chain that OOP books warned against?

The warning was about unchecked chains of instanceof and casts, where a new subtype silently falls through to the wrong branch. A switch over a sealed interface is exhaustive: if a new permitted type is added, every switch without a default stops compiling until it handles it. That turns the old risk into a compile error.

Should records have behaviour at all?

Yes, when the behaviour is about the record's own data, such as validation, derived values or small calculations. Records are classes and may have methods. What they should not do is hold mutable state or reach out to services, because they are meant to be transparent, immutable data.

How would you evaluate the candidate's statement in an interview setting?

I would treat it as an opening for discussion rather than a mistake. A strong candidate can explain the trade-off between the two styles, give an example of each, and say when they would choose which. Holding a firm view either way without that reasoning is the weaker signal.

8Conclusion

In this answer, we saw that code built on records, sealed interfaces and pattern matching keeps all four pillars. Records encapsulate immutable data and validate it in compact constructors, sealed interfaces are abstractions with a closed set of implementations, switch with patterns is polymorphism chosen by the caller, and inheritance is limited deliberately in favour of composition. The two styles solve different problems, closed variants with many operations versus open types with few operations, and good Java code picks per model.

What to say in the interviewAbout 90 seconds aloud

Open with

I'd respond that it's still object-oriented; it just uses a different default for where behaviour lives. Walking through the pillars: records encapsulate data, their fields are private and final, and a compact constructor can validate every value, so a record can never exist in an invalid state, which is stronger than a class with setters.

Then, if they want more

A sealed interface is an abstraction, callers use Parcel without knowing which kind it is, with the twist that the set of implementations is closed by the permits clause.

A switch with pattern matching is polymorphism: the same call behaves differently per type, but the caller lists the cases and the compiler checks exhaustiveness, instead of the JVM picking an overridden method. And inheritance is limited on purpose, which matches the long-standing advice to prefer composition. Then I'd explain when each style fits.

If variants are closed and stable but operations keep growing, like events, commands or API results, data-oriented code is better, because a new operation is one function and the compiler catches missing cases. If types are open and operations few, like plug-in channels or strategies, classic virtual dispatch is better, because a new type is just a new class.

Good Java code mixes them and chooses per model; a sealed interface can even declare a core abstract method while other operations use switches.

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 candidate says that Java code using records, sealed interfaces and pattern matching is...": https://scrivio-interview-library.netlify.app/java/oop-principles/16d6e5a688/