Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

Java has moved from deep class hierarchies and design-pattern-heavy code toward records, sealed interfaces and pattern matching, often called data-oriented programming. As the person who sets coding standards and design interview expectations for a large Java organization, what do you change and what do you keep?

I keep the core object-oriented ideas: encapsulated invariants, small interfaces and composition over inheritance. I change the defaults for data: records for immutable values, sealed interfaces for closed sets of variants, and pattern matching in switch for exhaustive handling instead of visitor boilerplate, all standard since Java 21. Design interviews then ask candidates to choose between polymorphism and sealed data with an explanation, standards apply to new code while legacy hierarchies change only when touched, and JPA entities stay mutable classes, so records go to DTOs and value objects.

Why this question is asked

Every few years a language change shifts what good Java looks like. Records arrived in Java 16, sealed classes in Java 17, and pattern matching for switch and record patterns in Java 21, which together make a style often called data-oriented programming practical: model data as plain immutable shapes and write operations over them, checked by the compiler. A leader who sets standards for hundreds of engineers must decide what to adopt, what to keep, and how to change interviews and existing code without chaos. In this answer, we will see what the new features give us with an example, decide what to keep from classic object-oriented design, explain how to choose between the two styles, and then plan the standards, the interview changes, the transition and the framework limits.

1What the New Features Give Us

A record is a class for immutable data: we list the components, and Java generates the constructor, accessors, equals, hashCode and toString. A sealed interface lists exactly which types may implement it, so the set of variants is closed and known to the compiler. Pattern matching in switch lets us test the type of a value and pull out its components in one step, and when we switch over a sealed type, the compiler checks that every variant is handled.

Before these features, the classic way to handle a closed set of shapes was the Visitor pattern: an accept method on every class, a visitor interface with one method per class, and a new visitor class for every operation. It works, but it needs a lot of boilerplate for a simple idea. Here is the same operation in the newer style.

Java
sealed interface PaymentResult permits Approved, Declined, NeedsReview {}
record Approved(String authCode, BigDecimal amount) implements PaymentResult {}
record Declined(String reason) implements PaymentResult {}
record NeedsReview(String caseId, Duration sla) implements PaymentResult {}
String describe(PaymentResult r) {
return switch (r) {
case Approved(var code, var amount) -> "approved " + amount + " (" + code + ")";
case Declined(var reason) -> "declined: " + reason;
case NeedsReview(var id, var sla) -> "review " + id + " within " + sla.toHours() + "h";
}; // no default: the compiler knows all three cases are covered
}
// describe(new Declined("insufficient funds")) -> "declined: insufficient funds"

Notice what happens when someone adds a fourth variant, say Refunded, to the permits list. Every switch like this one stops compiling until it handles Refunded. With a default branch or with instanceof chains, the new case would silently fall through. That compiler check is the main practical win, more than the shorter syntax.

2What We Keep From Object-Oriented Design

Data-oriented programming does not replace object-oriented design; it changes where we use each. Encapsulation of invariants stays essential. A record can and should validate its components in its compact constructor, so an Approved with a negative amount can never exist. Objects with behaviour and changing state, such as a connection pool or a shopping cart being edited, remain ordinary classes that hide their fields.

Small interfaces stay the main tool for open extension points: a PaymentGateway interface with implementations per provider is still the right design, because new providers arrive and each brings its own behaviour. Composition over inheritance stays too, and if anything the new features strengthen it, since records cannot extend other classes and sealed hierarchies are kept deliberately shallow.

3Choosing Between Polymorphism and Sealed Data

The decision rule I would teach is about which side is open. If the set of types is open, new kinds arrive often and each carries its own behaviour, use an interface and polymorphism: adding a type means adding a class, and no existing code changes. If the set of types is closed, the shapes are stable, but new operations over them arrive often, use sealed data and pattern matching: adding an operation means writing one new function, and the compiler shows every place that must handle a new shape if one is ever added.

For example, payment providers are open, so they are an interface. The results a payment can have are closed, so they are a sealed interface of records. Many real systems use both side by side, and a good engineer can say which situation they are in and why.

Which style fits
QuestionPolymorphism (interface)Sealed data + pattern matching
Which set grows?new types with their own behaviournew operations over fixed shapes
Adding a typeadd a class, nothing else changesadd a record, compiler flags every switch
Adding an operationchange every implementationwrite one function with a switch
ExamplePaymentGateway per providerPaymentResult: Approved, Declined, NeedsReview
Typical misusevisitor boilerplate for closed datagiant switch over an open, growing set

4Standards and Design Interviews

I would write the standard as defaults with reasons, not as bans. New value types and DTOs are records. Closed sets of variants are sealed interfaces, handled with exhaustive switches without a default branch, so the compiler can help. Visitor and deep abstract class hierarchies need a justification in review. Classes with mutable state keep classic encapsulation. Each rule comes with a short example from our own code base, because engineers follow examples more than prose.

For design interviews, I would update the exercises so the choice itself is assessed. A question like modelling payment results and payment providers gives the candidate a closed set and an open set at once. A strong candidate picks sealed records for one and an interface for the other and explains the trade-off; a candidate who uses one style for everything, either way, shows less judgement. We should not reward the newest syntax for its own sake. Interviewers get a short guide so that a candidate who writes a correct, well-reasoned visitor is not marked down, while one who can explain both styles gets credit.

5Planning the Transition

A large organisation has years of code with deep hierarchies, and rewriting it for style alone is risk without user value. So the standard applies to new code. Existing hierarchies stay until a change touches them; when a team modifies such code for a feature, they may refactor it toward the new style, protected by tests that existed or that they add first. A refactoring that changes behaviour by accident is far more expensive than a visitor that looks old.

Tooling helps the transition: IDE inspections and OpenRewrite-style automated refactorings can convert simple cases, such as data classes to records or instanceof chains to pattern matching, in bulk and safely, and code review catches the rest. We also need to check that every service runs on Java 21 or later; for teams still on older versions, the standard says what to do until they upgrade.

6Where the Frameworks Draw the Line

Frameworks set real limits. JPA entities, the classes Hibernate maps to tables, need a no-argument constructor, mutable fields that Hibernate can set, and usually a proxyable, non-final class for lazy loading. Records are final and immutable, so they cannot be entities. Records fit DTOs, projections from queries, value objects, and Hibernate embeddables in recent versions (check your version), but the entity itself stays a class.

Similarly, some serialization and mapping libraries need configuration or recent versions to work with records; Jackson supports them well today, and Spring's configuration properties bind to records. Our standard should list these cases explicitly, so teams do not discover them one failing test at a time.

7Follow-up questions

Where do records not fit well?

Records do not fit where an object has identity and changing state, such as JPA entities or objects with a lifecycle. They do not fit where we need inheritance of state, since a record cannot extend another class, or where we want to hide the representation, because a record's components are its public API: renaming a component changes the accessor and breaks callers. They also fit poorly for objects with many optional fields, where a builder or a class with clearer construction reads better than a constructor with twelve arguments.

Does sealed data break the open/closed principle?

It trades one kind of openness for another. A sealed hierarchy is closed to new types by design, but open to new operations, each written as a new function. The principle is still respected as long as we choose sealed data only for sets that really are closed, such as the outcomes of a payment.

8Conclusion

In this answer, we saw that records, sealed interfaces and pattern matching, standard since Java 21, make data-oriented code practical and give us compiler-checked handling of closed sets. We keep encapsulated invariants, small interfaces for open extension points and composition over inheritance. The standard and the interviews focus on choosing between polymorphism for open sets of types and sealed data for closed sets, the transition applies to new code and to legacy code only when it is touched, and JPA entities stay classes while records serve DTOs and value objects.

What to say in the interviewAbout 80 seconds aloud

Open with

I would keep the object-oriented ideas that still pay off: encapsulating invariants, small interfaces and composition over inheritance. What I would change is the default for data. Since Java 21 we have records, sealed interfaces and pattern matching in switch as standard features, so new value types and DTOs should be records, closed sets of variants should be sealed interfaces, and we handle them with exhaustive switches without a default, so the compiler flags every place when a variant is added.

Then, if they want more

That replaces a lot of visitor boilerplate. The key judgement I want engineers and candidates to show is choosing the style: when new types with their own behaviour keep arriving, like payment providers, use an interface and polymorphism; when the shapes are fixed but operations grow, like payment results, use sealed data.

I would update design exercises to contain both cases and train interviewers not to reward syntax for its own sake. For the transition, the standard applies to new code; legacy hierarchies change only when a feature touches them, with tests first, and automated refactoring for simple cases.

And I would document framework limits: JPA entities must stay mutable classes, so records fit DTOs, projections and value objects rather than entities.

About this answer

Last updated
12 November 2026
Versions it names
Java 21, Java 16, Java 17
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. 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 "Java has moved from deep class hierarchies and design-pattern-heavy code toward records...": https://scrivio-interview-library.netlify.app/java/coding-problems/d1ea67295f/