Quick revision
The short answer
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.
What to remember
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.
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.
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.
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.
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.
| Question | Virtual dispatch | Sealed type + switch |
|---|---|---|
| Where behaviour lives | In each class | In the operation, by case |
| Adding a new type | Easy: one new class | Edit every switch (compiler lists them) |
| Adding a new operation | Edit every class | Easy: one new function |
| Who can add types | Anyone | Only the permitted list |
| Good fit | Plug-ins, strategies, open sets | Events, commands, results, ASTs |
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.
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.
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.
- 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 "A candidate says that Java code using records, sealed interfaces and pattern matching is...": https://scrivio-interview-library.netlify.app/java/oop-principles/16d6e5a688/