An Introduction to Java Sealed Classes and Interfaces
In this article, we’ll explore what a sealed type in Java is, how to define a class and interface as sealed and finally we’ll go through a practical example. Sealed class syntax To have explicit control over the extensibility of a class we use identifier sealed and then permits to specify the set of classes allowed to extend: sealed class Advice permits BeforeAdvice, Interceptor, AfterAdvice { } non-sealed class BeforeAdvice extends Advice { } final class Interceptor extends Advice { } sealed class AfterAdvice extends Advice { } final class AfterReturningAdvice extends AfterAdvice { } ...