JEP proposed to target JDK 17: 409: Sealed Classes

Florian Weimer fw at deneb.enyo.de
Mon May 24 13:02:47 UTC 2021


* mark reinhold:

> The following JEP is proposed to target JDK 17:
>
>   409: Sealed Classes
>        https://openjdk.java.net/jeps/409
>
>   Summary: Enhance the Java programming language with sealed classes and
>   interfaces.  Sealed classes and interfaces restrict which other classes
>   or interfaces may extend or implement them.
>
> Feedback on this proposal from JDK Project Committers and Reviewers [1]
> is more than welcome, as are reasoned objections.  If no such objections
> are raised by 23:59 UTC on Wednesday, 12 May, or if they’re raised and
> then satisfactorily answered, then per the JEP 2.0 process proposal [2]
> I’ll target this JEP to JDK 17.

I've got a question regarding this example from the JEP:

| Sealing a class restricts its subclasses. User code can inspect an
| instance of a sealed class with an if-else chain of instanceof tests,
| one test per subclass; no catch-all else clause is needed. For
| example, the following code looks for the three permitted subclasses
| of Shape:
| 
| Shape rotate(Shape shape, double angle) {
|     if (shape instanceof Circle) return shape;
|     else if (shape instanceof Rectangle) return shape.rotate(angle);
|     else if (shape instanceof Square) return shape.rotate(angle);
|     // no else needed!
| }

With JEP 409, there is no way to make this actually compile with
suitable definitions for Shape/Circle/Rectangle/Square, right?  I
looked at the JLS changes, and I don't see a way they could enable
anything like this.

I expect functionality like this will be delived as part of JEP 406,
but with different syntax.


More information about the jdk-dev mailing list