Sealed types and pattern matching
22 questions in 8 subtopics.
Sealed classes and interfaces
Explain sealed classes and interfaces: syntax, what permitted subclasses must declare, and where they must live.When would you choose a sealed interface over an enum, over an open interface, or over an abstract class with a private constructor?A shared library exposes a sealed interfacePaymentEvent with five record subtypes. Consumers write exhaustive switches. A sixth subtype is needed. How do you handle the release?Sealed hierarchies give compile-time guarantees inside one codebase but become coordination points when shared between teams. How would you decide, as an organization-wide rule, where sealed types may appear in public APIs?Pattern matching for instanceof
Rewrite using pattern matching for instanceof and explain the scope of the binding variable:if (obj instanceof String) {
String s = (String) obj;
if (s.length() > 3) return s.toUpperCase();
}
public boolean equals(Object o) {
if (!(o instanceof Point)) return false;
Point p = (Point) o;
return x == p.x && y == p.y;
}
Switch pattern matching
Write a switch that returns a description for an Object: "null" for null, the length for a String longer than 10, "short string" otherwise, "int" for Integer, and "other" for anything else.Explain dominance rules in pattern switches and whycase CharSequence cs -> ...; case String s -> ... does not compile.Your team is replacing a Visitor pattern over an AST with sealed interfaces and pattern switches. What do you gain and lose, and how do you decide per module?Record patterns and deconstruction
Givenrecord Point(int x, int y) and record Line(Point a, Point b), write a method that returns true for vertical lines using record patterns.Record patterns deconstruct using accessor methods. What happens if a record overrides an accessor with side effects or a defensive copy, and why does that matter for pattern matching code?A team uses nested record patterns heavily in a message-routing service. Performance profiling and readability reviews raise concerns. What do you look for?Exhaustiveness checking
How does the compiler check exhaustiveness for switches over sealed types, and what happens at run time when a new permitted subclass appears without recompiling the switch?Exhaustiveness checking moves some errors from run time to compile time, but couples every switch to the full set of variants. How do you set org-wide guidance on using exhaustive switches versus default branches?Unnamed variables and patterns
Use unnamed variables and patterns (Java 22) to simplify each case: an unused catch parameter, an unused lambda parameter, and a record pattern where only one component matters.Explain how unnamed patterns help in switch cases over sealed types, for examplecase Circle _, Square _ -> ..., and why that was not allowed with named bindings.Primitive patterns
What is the status of primitive types in patterns, instanceof and switch in Java 25, and what doesif (value instanceof byte b) mean for an int value?A team asks to enable --enable-preview in production builds to use primitive patterns. What questions do you ask and what is your decision rule?Algebraic data types in Java
Model a payment result as an algebraic data type in Java 25 with success, declined with a reason, and failed with an exception, and show how callers consume it.Your organization is adopting data-oriented programming with sealed interfaces and records for domain modelling. Where does it fit with object-oriented design and Spring-based services, and where does it not?Java resisted algebraic data types and pattern matching for 25 years, then delivered them in stages from Java 14 to 21. Explain why it was done in that order (records, sealed, patterns) and what that tells you about betting on future language features.No question matches these choices.