Package modernfeatures.java17
Class SealedClassesDemo
java.lang.Object
modernfeatures.java17.SealedClassesDemo
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
andnon-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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
static class
static class
static final class
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
SealedClassesDemo
public SealedClassesDemo()
-
-
Method Details
-
main
Main entry point: creates instances of the permittedShape
subclasses, iterates through them, and uses a pattern-matching switch expression to print descriptive information about each shape.
-