Sealed Interfaces (introduced in Java 17)

 Sealed Interfaces (introduced in Java 17)


Sealed interfaces extend the concept of sealed classes to interfaces. They provide control over which classes can implement the interface, promoting better encapsulation and API design. Here's an example:


public sealed interface Shape permits Circle, Rectangle {

    void draw();

}


public final class Circle implements Shape {

    public void draw() {

        System.out.println("Drawing a circle.");

    }

}


public final class Rectangle implements Shape {

    public void draw() {

        System.out.println("Drawing a rectangle.");

    }

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...