176. Closing the electrical panel after JDK 17
Do you remember our electrical panel model introduced earlier in Problems 172 and 173? In Problem 173, we closed this model as much as possible by using the Java capabilities available before JDK 17. Now, we can revisit that model (Problem 173) and close it completely via JDK 17 sealed classes.
We start with the ElectricComponent
interface, which is declared as follows:
public interface ElectricComponent {}
At this moment, this interface is not closed. It can be extended/implemented from any other point of the application. But we can close it by transforming it into a sealed
interface with the proper permits
clause, as follows:
public sealed interface ElectricComponent
permits ElectricCircuit, ElectricBreaker,
Capacitor, Resistor, Transistor {}
Next, let’s focus on the semi-closed ElectricCircuit
class. This is an abstract
class that uses a package-private constructor to block any extension from...