[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)

Dec 2

AgentScope-Java uses Java 17's Records and sealed classes to perform type checking at compile time, avoiding the hidden risks of Python's dynamic typing in concurrent scenarios.

Unverified50%
Dec 2

Java sealed classes use the sealed keyword to restrict which classes can inherit from them, and together with switch pattern matching, the compiler can check whether branches are exhaustive.

Unverified50%
Nov 20

Combining Java 17 Record and sealed classes can constrain types at compile time and avoid runtime type errors.

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

Java sealed classes have been officially released since Java 17.

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%
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%

2 more timeline events

All Facts (12)

Unverified

AgentScope-Java uses Java 17's Records and sealed classes to perform type checking at compile time, avoiding the hidden risks of Python's dynamic typing in concurrent scenarios.

50%
Unverified

Java sealed classes use the sealed keyword to restrict which classes can inherit from them, and together with switch pattern matching, the compiler can check whether branches are exhaustive.

50%
Unverified

Combining Java 17 Record and sealed classes can constrain types at compile time and avoid runtime type errors.

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

Java sealed classes have been officially released since Java 17.

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%
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

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%

Source Articles