Class SealedClassesPreviewExamples

java.lang.Object
net.jrodolfo.java_evolution.java15.SealedClassesPreviewExamples

public class SealedClassesPreviewExamples extends Object
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.

  • Constructor Details

    • SealedClassesPreviewExamples

      public SealedClassesPreviewExamples()
  • Method Details

    • area

      public double area(SealedClassesPreviewExamples.Shape shape)
      Calculates the area of a known shape hierarchy.
      Parameters:
      shape - the shape to measure
      Returns:
      the shape area
    • permittedShapeNames

      public List<String> permittedShapeNames()
      Lists the classes explicitly permitted by the sealed SealedClassesPreviewExamples.Shape parent.
      Returns:
      permitted subtype simple names
    • java15ReflectionApiSource

      public String 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