175. Introducing the permits clause
In the previous problem, you saw how to write a closed hierarchical model in a single source file. Next, let’s use the Fuel.java source file to rewrite this model by using separate sources and separate packages.
Working with sealed classes in separate sources (same package)
Let’s consider the sealed Fuel interface from Fuel.java in package com.refinery.fuel:
public sealed interface Fuel {} // Fuel.java
We know that this interface is extended by three other interfaces: SolidFuel, LiquidFuel, and SolidFuel. Let’s define SolidFuel in the SolidFuel.java source (same package), as follows:
public sealed interface SolidFuel {} // SolidFuel.java
As you’ll see, this code will not compile (it is like the compiler is asking: hey, what’s the point of a sealed interface without any implementation/extension?). This time, we have to explicitly nominate the interfaces that can extend/implement the Fuel...