Java - Sealed Classes and Interfaces

Опубликовано: 12 Февраль 2026
на канале: KOBS Technologies
75
0

Prior to Java 17, there was no restriction on classes or interfaces regarding which all classes can inherit them. A public interface was available to be implemented by any class, and any public class was available to be extended by any other class – unless declared final.

Now with Java 17, a class or an interface can be declared sealed class or sealed interface using the modifier sealed. It is a preview feature in Java 15 through JEP 360.

A sealed class or interface restricts which other classes or interfaces may extend or implement them. Conceptually, it is a more declarative way than access modifiers to restrict the use of a class or interface as a parent class or as a parent interface.

The reserved keyword permits lists all the classes that can extend the sealed class directly. The listed subclasses can either be final, non-sealed, or sealed.