Exploring sealed interfaces
In Chapter 9, we learned that sealed
classes enable us to scope our inheritance hierarchy by specifying which classes can subtype our class. We used both the sealed
and permits
keywords as a pair to do this. Once a class has been sealed, each subclass of that class must be sealed
, non-sealed
, or final
– that is, we continue the sealed hierarchy (sealed
), end the sealed hierarchy (non-sealed
), or end the hierarchy altogether (final
).
It is also possible to seal interfaces. We will use the example from Chapter 9 with some small changes. Firstly, Figure 10.11 shows the relevant UML diagram, which will help explain the code:
Figure 10.11 – Sealed interface UML diagram
In this figure, we have an interface, indicated by <<interface>>
, called Driveable
. In UML, to specify that a class implements an interface, the <<realize>>
keyword is used (plus the dashed line with an arrow referring to the...