Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

sealed, permits and non-sealed

Reference for Java sealed classes and interfaces: restrict which types may extend them with permits, use final, sealed or non-sealed subtypes, and switch exhaustively.

A sealed class or interface restricts which other types may extend or implement it. The permits clause lists the allowed direct subtypes. Each permitted subtype must declare how it continues the hierarchy: final (no more subclasses), sealed (with its own permitted list), or non-sealed (open again).

Sealed types, finalized in Java 17, give you a closed set of alternatives. Together with records and pattern matching, switch over a sealed type can be checked for exhaustiveness without a default branch.

Java
sealed interface Shape permits Circle, Square { }
record Circle(double r) implements Shape { }
record Square(double s) implements Shape { }

Try it

Java An exhaustive switch over a sealed interface

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java final, sealed and non-sealed subtypes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
sealed class|interface Name [permits A, B] { }
final class A extends Name { }
sealed class B extends Name permits B1 { }
non-sealed class C extends Name { }
Rule Detail
Omitting permits Allowed when all subtypes are in the same source file; the compiler infers them.
Same module or package Permitted subtypes must be in the same module, or in the same package if the sealed type is in the unnamed module.
Subtype modifier Every permitted subtype must be final, sealed or non-sealed. Records and enums are implicitly final.
Direct supertype Every permitted subtype must directly extend or implement the sealed type.

Usage notes

  • non-sealed is a hyphenated keyword, valid only as a modifier in this position. sealed and permits are contextual keywords and can still be used as identifiers elsewhere.
  • Exhaustive switch: a switch expression over a sealed type needs a case for every permitted type (or a total pattern). Adding a new subtype later produces a compile error at every switch, which is the point.
  • Not the same as final: a sealed class can still have subclasses, just a known list of them.
  • Sealed interfaces are often paired with records for algebraic data types (Result, Expression, Event).
  • Cannot use with anonymous classes or lambdas: a sealed interface is not a functional-interface target.
  • Reflection: Class.isSealed() and getPermittedSubclasses() report the hierarchy.
  • Library design: sealing a public type stops users from extending it. Prefer it when you control all variants.