[KongchangAI]
ConceptSealed Classes

Sealed Classes

A language feature formally introduced in Java 17. A permits clause explicitly restricts the subtypes that may extend or implement a type, making the inheritance relationship controlled and closed, which in turn supports compile-time exhaustiveness checking.

Timeline (last 90 days)

Oct 18

Java sealed classes have been officially released since Java 17.

Unverified50%
Oct 18

Sealed classes explicitly declare the permitted subtypes via the permits clause, turning inheritance relationships from open to controlled.

Unverified50%
Oct 18

In combination with sealed classes, the compiler can verify whether all permitted subclasses in a switch are handled, and a missed subtype results in a compile error (exhaustiveness checking).

Unverified50%
Oct 18

Exhaustiveness checking is only effective for sealed hierarchies; for ordinary open interfaces, the compiler still requires a default branch.

Unverified50%
Oct 18

Adding a default branch in a switch disables exhaustiveness checking, so newly added subtypes no longer trigger compile errors; therefore, unnecessary default branches should be avoided when using sealed classes.

Unverified50%
Oct 18

Subtypes of sealed classes can carry their own distinct state and methods, giving them greater expressiveness than enums; if it is merely a fixed set of constants, an enum is the lighter-weight choice.

Unverified50%
Oct 18

The permits clause of a sealed class requires that subclasses reside in the same module (or the same package, depending on the module system configuration).

Unverified50%
Oct 18

Mainstream IDEs already support prompting for missing branches in sealed hierarchies, forming a dual safeguard together with the compiler's exhaustiveness checking.

Unverified50%
Oct 18

According to Hacker News discussion, many developers believe the most valuable use case for sealed classes is domain modeling—expressing business states with a closed set of variants.

Unverified50%

All Facts (9)

Unverified

Java sealed classes have been officially released since Java 17.

50%
Unverified

Sealed classes explicitly declare the permitted subtypes via the permits clause, turning inheritance relationships from open to controlled.

50%
Unverified

In combination with sealed classes, the compiler can verify whether all permitted subclasses in a switch are handled, and a missed subtype results in a compile error (exhaustiveness checking).

50%
Unverified

Exhaustiveness checking is only effective for sealed hierarchies; for ordinary open interfaces, the compiler still requires a default branch.

50%
Unverified

Adding a default branch in a switch disables exhaustiveness checking, so newly added subtypes no longer trigger compile errors; therefore, unnecessary default branches should be avoided when using sealed classes.

50%
Unverified

Subtypes of sealed classes can carry their own distinct state and methods, giving them greater expressiveness than enums; if it is merely a fixed set of constants, an enum is the lighter-weight choice.

50%
Unverified

The permits clause of a sealed class requires that subclasses reside in the same module (or the same package, depending on the module system configuration).

50%
Unverified

Mainstream IDEs already support prompting for missing branches in sealed hierarchies, forming a dual safeguard together with the compiler's exhaustiveness checking.

50%
Unverified

According to Hacker News discussion, many developers believe the most valuable use case for sealed classes is domain modeling—expressing business states with a closed set of variants.

50%

Source Articles