Изменения в новых версиях / Java 17

// *** in version 17: ***
sealed interface Event permits ClickKeyPressClose { }

record Click(int x, int y) implements Event { }   // a record is final already
record KeyPress(char key) implements Event { }
record Close() implements Event { }

// permits may be dropped when every heir lives in the same file:
// sealed interface Event { }

Event event = new KeyPress('a');
String text;
if (event instanceof Click c) {
    text = "click " + c.x() + "," + c.y();
else if (event instanceof KeyPress k) {
    text = "key " + k.key();
else {
    text = "close";
}
System.out.println(text);

// the compiler knows the full list of the heirs, and since version 21
// this chain becomes a switch without default