Java 21 makes me like Java again
The &quot;Sealed classes&quot; feature, as described here, just feels all wrong to me.<p>They are saying that if you have a (normal) interface, anyone can create a new class implementing it. So if you do<p><pre><code>    if (x instanceof Foo) {
        ...
    } else if (x instanceof Bar) {
        ...
    } ...
</code></pre>
then your code will break at runtime if someone adds a new class, as that code won&#x27;t expect it. So the article is saying the solution is to use the new &quot;sealed&quot; interfaces feature, so nobody can create any new classes implementing that interface, and your &quot;if&quot; statement will not break.<p>Surely object-oriented programming already thought about that and already solved that? (I know object-oriented programming is out of vogue at the moment, but Java <i>is</i> an object-oriented language.)<p>The solution there is to add a method to the interface, and all your classes implement that. Then rather than having a massive if&#x2F;switch statement with all the options, you call the method.<p>That is better than preventing people from extending your code, it <i>allows</i> them to extend your code. They just have to implement the method. And the compiler will force them to do that, so they can&#x27;t even accidentally forget.<p>The example given of color spaces (RGB, CMYK etc.) is a great example. I can absolutely imagine writing code which uses color spaces, but then some user or client having a need to use a weird obscure color space I haven&#x27;t thought of. I wouldn&#x27;t want to restrict my code to saying &quot;these are the color spaces I support, due to this massive if&#x2F;switch statement listing them all, the code is written in such a way that you can&#x27;t extend it&quot;.