Java language

sealed class

Restricts direct subclasses while allowing selected closed, sealed or reopened branches. Java 17+. Sealing does not enforce object invariants or authorize access.

Formal Syntax & Structure

java syntaxjava
sealed abstract class Notice permits Receipt, CustomNotice { ... }

Working Example

Example Snippetjava
public class Main {    static sealed abstract class Notice permits Receipt, CustomNotice {        abstract String label();    }    static final class Receipt extends Notice {        String label() { return "receipt"; }    }    static non-sealed class CustomNotice extends Notice {        String label() { return "custom"; }    }    static final class Reminder extends CustomNotice {        @Override String label() { return "reminder"; }    }    public static void main(String[] args) {        Notice a = new Receipt();        Notice b = new Reminder();        System.out.println(a.label());        System.out.println(b.label());    }}
Java Reference