A sealed interface restricts which classes or interfaces may directly implement or extend it. This creates a closed type hierarchy that can replace chains of instanceof checks with compiler-checked exhaustive processing. section 8.2
Exhaustive processing with a sealed interface
sealed interface Entry {} record IntEntry(int value) implements Entry {} record StringEntry(String value) implements Entry {} void processList(List<Entry> list) { for (Entry element : list) { switch (element) { case IntEntry(int number) -> processNumber(number); case StringEntry(String string) -> processString(string); } } } The compiler checks that the switch covers every permitted Entry type. Adding another inheritor produces a compilation error at the switch, forcing the new case to be handled. section 8.2
Use sealed hierarchies when processing must account for every known variant:
- Prefer an explicit exhaustive switch over a chain of instanceof checks when the possible types form a closed hierarchy. section 8.2
- Use an explicit failure branch when unrelated objects may still appear, so unexpected values are easier to investigate. section 8.2

