Class SealedClassesPreviewExamples
java.lang.Object
net.jrodolfo.java_evolution.java15.SealedClassesPreviewExamples
Demonstrates sealed classes as a Java 15 preview feature.
Before sealed classes, a type hierarchy was usually open to any subtype that could access the parent type. That made it difficult to model small closed domains directly in the type system.
Sealed classes solve this by letting the parent type explicitly list permitted subtypes. They became final in Java 17. This project compiles on JDK 25, so the example uses final syntax while documenting the Java 15 preview origin.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordCircle implementation permitted bySealedClassesPreviewExamples.Shape.static final recordRectangle implementation permitted bySealedClassesPreviewExamples.Shape.static interfaceSealed parent type that explicitly lists permitted implementations. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondoubleCalculates the area of a known shape hierarchy.
-
Constructor Details
-
SealedClassesPreviewExamples
public SealedClassesPreviewExamples()
-
-
Method Details
-
area
Calculates the area of a known shape hierarchy.- Parameters:
shape- the shape to measure- Returns:
- the shape area
-