dictionary meaning - a device or substance that is used to join two things together so as to prevent them from coming apart or to prevent anything from passing between them.
Prevent anything from passing between them. That's exactly what sometimes you want to put in place. When you have an interface and want to restrict other interfaces to extend or classes to implement to have a control on, you ned to seal your interface by specifying all those are permitted to extend or implement.
Java sealed interfaces is a feature introduced in Java 15 as a preview feature and became a standard feature in Java 17. Sealed interface restricts which classes or interfaces can implement or extend it. Classes that implement a sealed interface must be declared as final, sealed, or non-sealed. This provides more control over the inheritance hierarchy and helps to enforce certain design constraints.
To declare a sealed interface, use the sealed keyword followed by the permits clause, which lists the permitted subtypes.
E.g.
public sealed interface Shape permits Circle, Rectangle, Triangle {
double area();
}
Each permitted subtype must be declared as one of the following:
- Final: Cannot be extended further.
- Sealed: Can specify its own permitted subtypes.
- Non-Sealed: Removes the sealing restriction, allowing any class to extend it.
// Final class
public final class Circle implements Shape {
...
}
// Sealed class
public sealed class Rectangle implements Shape permits Square {
...
}
// Non-sealed class, additional permitted sub-type
public final Square extends Rectangle {
...
}
Benefits of Sealed Interfaces
Enhanced Control: Provide more control over the inheritance hierarchy, ensuring that only specific classes can implement the interface.
Improved Maintainability: By restricting the set of permitted subtypes, you can make your codebase easier to understand and maintain.
Better Exhaustiveness Checking: Sealed interfaces improve exhaustiveness checking in switch statements, especially when used with pattern matching (introduced in later Java versions).
The exhaustive checking in switch statement itself is very useful feature to have that makes your code not to miss handling a case of interface type in switch which otherwise is prone to bugs. The compiler would not let your code compile until all possible cases are handled in a switch statement making your code robust.
Shape aShape;
...
switch(aShape) {
case Circle circle -> circle.radius();
case Rectangle rectangle -> // do something
// handle all remaining cases or provide default case, otherwise you code fails compilation
}
Gotcha - Mockito, mocking sealed interface
Mocking is common in unit testing. If you are writing unit test for an object A that depends on object B, you will not be interested in B and can simply mock it's behavior. If Mockito is your mocking framework, and B happens to be a sealed interface with some permitted implementations, then you will not be able to mock like usually you do as follows:
class ATest {
...
@Mock
private B objB;
...
}
Your test fails with the following error when it is run:
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: interface B.
If you're not sure why you're getting this error, please open an issue on GitHub.
Java : 22
JVM vendor name : Amazon.com Inc.
JVM vendor version : 22.0.1+8-FR
JVM name : OpenJDK 64-Bit Server VM
JVM version : 22.0.1+8-FR
JVM info : mixed mode, sharing
OS name : Mac OS X
OS version : 13.6.6
You are seeing this disclaimer because Mockito is configured to create inlined mocks.
You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.
Underlying exception : org.mockito.exceptions.base.MockitoException: Unsupported settings with this type 'B'
Solution
Change mock to a specific implementation of the interface.
private final B objB = mock(BImpl.class); // sealed interface, specify specific implementation class to be mocked
No comments:
Post a Comment