5 ms·
You're right that in the simplified example you have probably nicer alternatives, but having sealed clases where you know that others can't expand has its benef
by estebank 6y ago
You're right that in the simplified example you have probably nicer alternatives, but having sealed clases where you know that others can't expand has its benefits to design some nice APIs, like state machines and type safe builders.
The benefits of enums (as these are referred to in some languages) is that after the safe downcast you have access to the fields and methods of the specific type just by invoking them.
Pattern matching also makes this construct even nicer to use (and I would argue that having one without the other is worse than not having either), but that doesn't seem to be included in the language yet, very probably for good reasons (I just don't follow Java development closely so I don't know about them).
- jdc 6y agoPattern matching is available as a preview feature: https://openjdk.java.net/jeps/375 https://openjdk.java.net/jeps/375