Sealed classes, introduced in Java 15 as a preview feature and made permanent in Java 17, allow developers to control which classes can extend or implement them. This feature is part of Project Amber, which aims to improve developer productivity by enhancing the Java language with small but powerful features.
Sealed classes are a new kind of class that restricts which other classes or interfaces can extend or implement them. This is done to provide a more robust and maintainable type hierarchy. When you define a sealed class, you specify a set of permitted subclasses.
To define a sealed class, you use the sealed keyword and specify the permitted subclasses with the permits clause. Here’s an example:
public sealed class Shape permits Circle, Rectangle, Square { // class body } public final class Circle extends Shape { // class body } public final class Rectangle extends Shape { // class body } public final class Square extends Shape { // class body }
In this example, Shape is a sealed class, and only Circle, Rectangle, and Square are permitted to extend it. Each subclass must be final, sealed, or non-sealed.
Sealed classes can be used to model hierarchical structures where the set of subclasses is known and fixed. This is particularly useful in domain modeling and when working with algebraic data types.
Here is an example of using sealed classes with pattern matching:
public class SealedClassExample { public static void main(String[] args) { Shape shape = new Circle(5); String result = switch (shape) { case Circle c -> "Circle with radius " + c.radius(); case Rectangle r -> "Rectangle with width " + r.width() + " and height " + r.height(); case Square s -> "Square with side " + s.side(); }; System.out.println(result); } }
In this example, we use a switch expression to handle different types of Shape. The compiler can ensure that all possible cases are covered because Shape is sealed.
Sealed classes are a powerful addition to Java, providing better control over class hierarchies and enhancing code readability and maintainability. By using sealed classes, you can create more robust and well-defined type systems in your Java applications.
The above is the detailed content of Mastering Sealed Classes in Java. For more information, please follow other related articles on the PHP Chinese website!