sealed classes, records and enums

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.

6 thoughts on “sealed classes, records and enums

  1. 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)

  2. 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.

  3. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *