Sealed classes and interfaces: Improve the reliability of code by preventing unexpected subclasses!
Problem Statement
Suppose you are developing a library in Java that includes a Shape class. The Shape class has several subclasses, including Circle, Rectangle, Triangle, and Polygon.
You have noticed that some of the subclasses are more likely to cause bugs than others. For example, the Triangle and Polygon classes are more complex and have more corner cases to consider. You want to ensure that any new subclasses that are added to the library are carefully considered and properly tested so that they do not introduce bugs or break existing functionality.
To address this problem, you can use the sealed keyword to restrict the set of permitted subclasses of Shape. For example, you can declare the Shape class as sealed and permit only the Circle, Rectangle, and Triangle classes to be its subclasses. Any attempt to create a new subclass that is not listed in the permits clause will result in a compilation error.
Benefits
This approach has several benefits. First, it makes it clear to other developers which subclasses are intended to be part of the public API of the library. Second, it ensures that any new subclasses are carefully considered and properly tested before they are added to the library. Finally, it makes it easier to reason about the behavior of the Shape class and its subclasses, since you know exactly which subclasses are permitted and what their behavior should be.
Introduction
Sealed classes and interfaces are a new feature introduced in Java 15 and further improved in Java 17. They provide a way to restrict the set of subclasses that can be created for a particular class or the set of classes that can implement an interface.
The purpose of sealed classes and interfaces is to make it easier to create robust and maintainable code. By restricting the set of subclasses or implementers, sealed types can help prevent bugs caused by unexpected behavior from unanticipated subclasses. This can also make it easier to reason about code and make changes in the future.
To create a sealed class or interface, you use the sealed keyword in the class or interface declaration, followed by a list of permitted subclasses or implementers. For example:
public sealed class Shape permits Circle, Rectangle
// class definition
}
public final class Circle extends Shape {
// class definition
}
public final class Rectangle extends Shape {
// class definition
}
{
In this example, the Shape class is declared as sealed, and it permits only the Circle and Rectangle classes to be its subclasses. Any attempt to create a new subclass that is not listed in the permits clause will result in a compilation error.
Note that the permits clause can also be used to permit sealed subclasses that are defined in other packages.
Recommended by LinkedIn
In addition to the permits clause, sealed classes and interfaces also introduce a new non-sealed keyword, which can be used to create a non-sealed subclass of a sealed class. A non-sealed subclass is a regular subclass that can be extended by any class, without the need for permission from the sealed class.
In addition to the permits clause, sealed classes and interfaces also introduce a new non-sealed keyword, which can be used to create a non-sealed subclass of a sealed class. A non-sealed subclass is a regular subclass that can be extended by any class, without the need for permission from the sealed class.
public sealed class Shape permits Circle, Rectangle
// class definition
}
public final class Circle extends Shape {
// class definition
}
public non-sealed class Triangle extends Shape {
// class definition
}
public final class EquilateralTriangle extends Triangle {
// class definition
}
{
In this example, the Triangle class is defined as a non-sealed subclass of Shape. This means that any class can extend Triangle, even though it is not listed in the permits clause of Shape. However, the EquilateralTriangle class is still required to be a subclass of Triangle, because it extends it directly.
Sealed classes and interfaces are a powerful new feature that can help improve the maintainability and reliability of Java code. By restricting the set of permitted subclasses and implementers, they can help prevent bugs caused by unexpected behavior from unanticipated subclasses. They can also make it easier to reason about code and make changes in the future.
In case you are a business seeking software solutions services, including this one, you may access the link provided below to see how eInfochips is helping its customers.