How to write a Sealed Interface ?
Finally, we've come to the last part of the Sealed concept. We've already learnt - what, why and how parts in last three articles.
It's time to see the second part of 'how' which is writing Sealed Interfaces.
Just like classes, an interface can be sealed by using sealed modifier along with permits clause like below -
sealed interface Celestial permits Planet, Star, Comet { ... }
One more example - package com.example.expression;
package com.example.expression
public sealed interface Expr permits ConstantExpr, PlusExpr { ... }
public final class ConstantExpr implements Expr { ... }
public final class PlusExpr implements Expr { ... }
That's it, we're done! Thanks for reading this far !
Ending by asking a question about what we learnt, see if you can answer it?
Can a class be non-sealed and final at the same time?