Class SealedClassesExamples
java.lang.Object
net.jrodolfo.java_evolution.java17.SealedClassesExamples
Demonstrates sealed classes, finalized in Java 17.
Before sealed classes, Java could not directly express a closed hierarchy in the type system. A parent type was usually open to any accessible subtype, even when the domain had a known finite set of valid implementations.
A sealed type solves this by explicitly controlling which classes or interfaces may implement or extend it. This makes domain hierarchies easier to understand and safer to exhaustively handle.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordFinal permitted implementation.static final recordFinal permitted implementation.static interfaceSealed parent interface that permits only the listed implementations.static classNon-sealed implementation, showing that a permitted subtype may reopen the hierarchy. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondoublearea(SealedClassesExamples.Shape shape) Calculates the area for a known sealed shape hierarchy.
-
Constructor Details
-
SealedClassesExamples
public SealedClassesExamples()
-
-
Method Details
-
area
Calculates the area for a known sealed shape hierarchy.- Parameters:
shape- the shape to measure- Returns:
- the calculated area
-