Java Snippet
Sealed Interfaces with Pattern Matching
Difficulty: Hard
Sealed interfaces (Java 17+) plus switch pattern matching (Java 21+) give Java algebraic-data-type ergonomics: a closed family of subtypes that the compiler can check exhaustively in one switch. This snippet shows the modern syntax in comments and a Java 13-compatible equivalent using a visitor-style abstract class hierarchy that compiles in our test runner. Use this pattern for tagged unions like `Result<Ok, Err>`, AST nodes, and state machines.
