Java sealed classes
Goals of sealed classes:
- Allow the author of a class or interface to control which code is responsible for implementing it.
- Provide a more declarative way than access modifiers to restrict the use of a superclass.
- Support future directions in pattern matching by providing a foundation for the exhaustive analysis of patterns.
Syntax
Option1: define inner classes:
public abstract sealed class Chinese {
final class SimplifiedChinese extends Chinese{}
final class TradictionalChinese extends Chinese{}
}
// a sealed class could not be extended, otherwise you get an compiler error:
// public class Other extends Chinese {}
// 'Other' is not allowed in the sealed hierarchy
Option2: