Class SealedClassesDemo

java.lang.Object
modernfeatures.java17.SealedClassesDemo

public class SealedClassesDemo extends Object
Demonstrates Java 17+ sealed classes, which restrict which classes can extend a superclass.

This example shows:

  • How to declare a sealed class (Shape) and specify the permitted subclasses.
  • Usage of final and non-sealed subclasses to control the inheritance hierarchy.
  • How pattern matching with switch expressions can safely handle all permitted subclasses, providing exhaustive type checking at compile time.
  • Constructor Details

    • SealedClassesDemo

      public SealedClassesDemo()
  • Method Details

    • main

      public static void main(String[] args)
      Main entry point: creates instances of the permitted Shape subclasses, iterates through them, and uses a pattern-matching switch expression to print descriptive information about each shape.