Sealed Class (Java 17+) hạn chế những class nào được phép extend nó — khai báo qua permits.
Ví dụ: public sealed class Shape permits Circle, Rectangle, Triangle {}. Mỗi subclass phải là final, sealed, hoặc non-sealed. Lợi ích kết hợp Pattern Matching: compiler biết tất cả subtypes nên có thể kiểm tra exhaustiveness — switch expression không cần default nếu đã xử lý hết.
Ví dụ: switch (shape) { case Circle c -> ...; case Rectangle r -> ...; case Triangle t -> ...; } — compiler báo lỗi nếu bỏ sót một case. Tốt hơn abstract class ở điểm: đóng gói hierarchy chặt hơn, phòng tránh subtype ngoài ý muốn. Ứng dụng: domain model, error type, state machine.
Sealed Classes (Java 17+) restrict which classes can extend them — declared via permits.
Example: public sealed class Shape permits Circle, Rectangle, Triangle {}. Each subclass must be final, sealed, or non-sealed. Benefit with Pattern Matching: compiler knows all subtypes and can verify exhaustiveness — switch expressions need no default when all cases are handled.
Example: switch (shape) { case Circle c -> ...; case Rectangle r -> ...; case Triangle t -> ...; } — compiler errors on missing cases. Better than abstract classes: tighter hierarchy encapsulation, prevents unintended subtypes. Use cases: domain models, error types, state machines.