Sealed Classes (introduced in Java 15)
Sealed classes restrict the inheritance hierarchy of a class, allowing you to specify which classes can extend it. This promotes better control and helps prevent unintended subclassing. Here's an example:
public sealed class Shape permits Circle, Rectangle {
// Common shape properties and methods
}
public final class Circle extends Shape {
// Circle-specific properties and methods
}
public final class Rectangle extends Shape {
// Rectangle-specific properties and methods
}
No comments:
Post a Comment