On this page
Classes, Records & Sealed Hierarchies
Prev: Language & OOP | Up: Language & OOP | Next: Generics & Type Erasure
Rel: record, sealed, interface, enum, switch expressions, Modern Data Modeling Tutorial
Ref: Java 21 Language Guide , Dev.java — Data-Oriented Programming
Tags: #public #java #oop #data-modeling #tier/core
Overview
Modern Java (Java 17–21+) pairs traditional class-based OOP with algebraic data types built from record, sealed, and exhaustive pattern-matching switch. Choosing the right construct makes invariants explicit at compile time and removes boilerplate.
Choosing a Type Construct
| Construct | Mutability & Identity | Inheritance | Best For |
|---|---|---|---|
class |
Mutable or immutable; reference identity | Open (unless final or sealed) |
Stateful services, entities with lifecycle, encapsulated mutable state |
record |
Shallowly immutable; value-based equals/hashCode |
Implicitly final; can implement interfaces |
Transparent data carriers, DTOs, domain value objects |
sealed interface / sealed class |
Determined by permitted subtypes | Closed set via permits |
Closed variant trees (sum types), protocol messages, AST nodes |
enum |
Fixed singleton instances | Implicitly final (per-constant class bodies allowed) |
Fixed sets of named constants with optional behavior |
interface |
Stateless contract (plus default/static/private methods) |
Multiple inheritance of type | Capabilities, behavioral abstractions, functional interfaces |
Compact Constructors & Invariant Validation
Records support a compact constructor that validates or defensively copies components before the compiler assigns them to the private final fields:
import java.util.List;
import java.util.Objects;
public record Order(String id, List<String> items) {
public Order {
Objects.requireNonNull(id, "id must not be null");
if (id.isBlank()) {
throw new IllegalArgumentException("id must not be blank");
}
// Defensive immutable copy prevents caller mutation after construction
items = List.copyOf(items);
}
}
Sealed Hierarchies & Exhaustive Pattern Matching
Combining sealed interface, record, and Java 21 switch expressions with record deconstruction gives compile-time exhaustiveness without needing a default branch:
public sealed interface PaymentResult permits Approved, Declined, Pending {}
public record Approved(String transactionId, long amountCents) implements PaymentResult {}
public record Declined(String reasonCode) implements PaymentResult {}
public record Pending(int retryAfterSeconds) implements PaymentResult {}
static String describe(PaymentResult result) {
return switch (result) {
case Approved(var txId, var cents) -> "Approved " + txId + " ($" + (cents / 100.0) + ")";
case Declined(var code) -> "Declined (" + code + ")";
case Pending(var waitSec) -> "Pending, retry in " + waitSec + "s";
};
}
If a new subtype is added to permits, every exhaustive switch fails to compile until updated.
Composition Over Inheritance
- Prefer implementing small
interfacecontracts and delegating to composed collaborators rather than building deep class hierarchies. - Mark concrete classes
finalunless explicitly designed and documented for subclassing. - Use
privateor package-private visibility by default; expose only the minimalpublicAPI surface.
Linked from
- tuts/java/builtins/AutoCloseable
- tuts/java/builtins/enum
- tuts/java/builtins/final
- tuts/java/builtins/finalize
- tuts/java/builtins/interface
- tuts/java/builtins/Object
- tuts/java/builtins/Object_wait_notify
- tuts/java/builtins/primitives
- tuts/java/builtins/record
- tuts/java/builtins/RuntimeException
- tuts/java/builtins/sealed
- tuts/java/builtins/static
- tuts/java/builtins/String
- tuts/java/builtins/switch_expressions
- tuts/java/builtins/try_with_resources
- tuts/java/builtins/var
- tuts/java/language_and_oop
- tuts/java/language_and_oop/generics_and_type_erasure
- tuts/java/tutorials/modern_data_modeling_records_and_sealed
- tuts/third_party/java/spring_framework
Related notes
- tuts/java/builtins/enum Linked here; links to this page; shared tags: #java, #tier
- tuts/java/builtins/interface Linked here; links to this page; shared tags: #java, #tier
- tuts/java/builtins/record Linked here; links to this page; shared tags: #java, #tier
- tuts/java/builtins/sealed Linked here; links to this page; shared tags: #java, #tier
- tuts/java/builtins/switch_expressions Linked here; links to this page; shared tags: #java, #tier
- tuts/java/language_and_oop Linked here; links to this page; shared tags: #java, #oop
- tuts/java/language_and_oop/generics_and_type_erasure Linked here; links to this page; shared tags: #java, #tier
- tuts/java/tutorials/modern_data_modeling_records_and_sealed Linked here; links to this page; shared tags: #java, #tier
- tuts/java/builtins/AutoCloseable links to this page; shared tags: #java, #tier
- tuts/java/builtins/final links to this page; shared tags: #java, #tier