Sealed Classes in Java

 Sealed Classes in Java


Java 15 introduced Sealed Classes, which provide a way to restrict the inheritance hierarchy of a class to a finite set of subclasses. Sealed classes are declared using the sealed modifier, and their permitted subclasses are declared using the permits keyword.

Example:



public sealed class Shape permits Circle, Rectangle, Triangle {

    // ...

}


public final class Circle extends Shape {

    // ...

}


public final class Rectangle extends Shape {

    // ...

}


public final class Triangle extends Shape {

    // ...

}

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...