Sealed Classes

Sealed classes restrict which classes can inherit them, enhancing encapsulation and giving more control.

public abstract sealed class Shape permits Circle, Rectangle {
    // Common methods and fields
}

public final class Circle extends Shape {
    // Circle-specific implementation
}

public final class Rectangle extends Shape {
    // Rectangle-specific implementation
}

Introduced in Java 17.