OpenJDK Terminology

Sealed Classes

July 03, 2026

Sealed classes, introduced as a preview in Java 15 and finalised in Java 17, let you explicitly control which classes or interfaces are permitted to extend or implement a given type. The sealed modifier on a class is paired with a permits clause listing the allowed subtypes.

public sealed interface Shape permits Circle, Rectangle, Triangle {}

Each permitted subtype must itself be declared final, sealed, or non-sealed. This makes the type hierarchy closed and knowable at compile time.

The main benefit is enabling exhaustive switch expressions. If you switch over a sealed Shape, the compiler can verify that you have handled every permitted subtype and will warn you if you add a new subtype without updating existing switch statements. This eliminates an entire class of bugs that previously required discipline and code review to catch.

Sealed classes work best in combination with records (for the leaf types) and pattern matching (for the switch logic).

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Comments (0)

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

No comments yet. Be the first.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard