Sometimes we write something for the book, only to realize it shouldn’t be in the book. This post is an example. Do you think this legal?
public sealed interface MySealedClass permits MyRecord, MyEnum {
}
record MyRecord() implements MySealedClass {}
enum MyEnum implements MySealedClass { VALUE }
It is! A sealed interface can be implemented by any Java type. Which means records and enums are included.
Hi, Can you illustrate this with a working java code
Deepak: This is working Java code; it compiles. What do you mean? (Also, it is unlikely someone would write this code. It is meant to demonstrate legality)
I meant a real world scenario where this code work
Deepak: I can’t think of one. The point of the example is to show it is legal.
Hello, I miss the point why it shouldn’t be on the book – since it is legal code and that was what you wanted to demonstrate.
Peter: It is legal code. But it’s off topic for the exam. We can’t put everything in the book. So sometimes good stuff has to be cut. I didn’t want to leave this on the cutting room floor so I blogged about it.