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. The sealed-type syntax shown here is also faithful to the Java 15 preview, while the documentation identifies its preview status.
-
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 withpermits. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondoubleCalculates the area of a known shape hierarchy.Returns the reflection API shape from the Java 15 first preview.Lists the classes explicitly permitted by the sealedSealedClassesPreviewExamples.Shapeparent.
-
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
-
permittedShapeNames
Lists the classes explicitly permitted by the sealedSealedClassesPreviewExamples.Shapeparent.- Returns:
- permitted subtype simple names
-
java15ReflectionApiSource
Returns the reflection API shape from the Java 15 first preview.The preview method is preserved as source text because the current JDK exposes the later
getPermittedSubclasses()API instead.- Returns:
- Java 15 preview reflection source
-