Skip to main content

Posts

Showing posts with the label sealed class

What is Sealed Classes in Java 17

Java 17, released in September 2021, introduced several new features and enhancements. While some of the most prominent changes in Java 17 have received widespread attention, here's a lesser-known unique feature: 1. Sealed Classes: Sealed classes are a feature that allows developers to control the extent to which other classes can inherit from them. By declaring a class as sealed, you can specify which other classes are allowed to subclass it. This feature enhances encapsulation and provides more control over class hierarchies and extensions. Sealed classes are declared using the `sealed` keyword, and the permitted subclasses are specified using the `permits` keyword. By default, a sealed class allows subclasses only from within the same module. However, you can also explicitly specify other modules that are permitted to subclass the sealed class. This feature enables developers to create more secure and maintainable code by restricting inheritance to a defined set of classes, prev