Records & Sealed Classes — Interview Questions
⚡ Short Answer
A record is a concise, immutable data carrier: you declare the components and the compiler generates the constructor, accessors, equals/hashCode, and toString. A sealed class/interface restricts which types may extend or implement it (permits ...). Together they enable data-oriented programming: sealed hierarchies model a closed set of variants (algebraic data types), and pattern-matching switch handles them exhaustively — the compiler errors if you miss a case.
☕Coffee Chat Question
Concept Made Simple
“What are Records and Sealed Classes, and how do they enable data-oriented programming?”
🧠Mind Map Answer
Remember It Faster
Records kill boilerplate for DTOs, value objects, and multiple return values. The compact constructor is where you validate invariants and defensively copy mutable components (records are only shallowly immutable).
Sealed + records + switch = algebraic data types in Java: model a fixed set of shapes (Circle, Square, Triangle), and a pattern-matching switch over the sealed type is checked for exhaustiveness — add a variant and every switch that forgot it fails to compile.
Key takeaway: records give you cheap immutable data; sealed types give you closed hierarchies; pattern matching makes handling them exhaustive and safe. That triad is modern, data-oriented Java.
⌨️Hands-on Keyboard
Learn by Doing
sealed interface Shape permits Circle, Square {}
record Circle(double r) implements Shape {}
record Square(double side) implements Shape {}
static double area(Shape s) {
return switch (s) { // exhaustive — no default needed
case Circle c -> Math.PI * c.r() * c.r();
case Square sq -> sq.side() * sq.side();
};
}
// Add a new permitted Shape → this switch fails to compile until handled.🔥What If?
Think Beyond the Expected
How do sealed classes make a pattern-matching switch safer than a normal class hierarchy?
Because the compiler knows the COMPLETE set of permitted subtypes, it can verify a switch covers every case — so you can omit the default branch and, crucially, when someone adds a new permitted subtype later, every switch that doesn't handle it stops compiling. With an open hierarchy the compiler can't know all subtypes, so it forces a default that silently swallows new cases at runtime. Sealed types turn 'did I handle every variant?' from a runtime hope into a compile-time guarantee.
😂Real World
Records are now the default for DTOs, API request/response models, events, and value objects — less boilerplate, correct equals/hashCode for free. Sealed hierarchies model domain state machines and result types (Success/Failure), and exhaustive switches mean adding a new state is a compile error everywhere it must be handled — a big maintainability win in large codebases.
🎯Interviewer's Expectation
Keywords they're listening for:
⚠️Common Mistakes
- ✗Assuming a record with mutable components is deeply immutable
- ✗Adding a default to a sealed switch (defeats exhaustiveness)
- ✗Using records where identity/mutability is actually required
✅Best Practices
- ✓Use records for immutable DTOs and value objects
- ✓Validate and defensively copy in compact constructors
- ✓Model closed variant sets with sealed types + exhaustive switch
🔁Follow-up Questions
- 1How do you validate or defensively copy in a record?
- 2Why are records only shallowly immutable?
- 3How does record deconstruction in switch patterns work?
🧩Related Technologies
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Modern Java (Advanced Java) Interview question: "What are Records and Sealed Classes, and how do they enable data-oriented programming?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
⭐ Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.