I looked at the source code of the Enum
class . It looks like a simple abstract class with a protected constructor. This is not the final version, it does not have any special annotations, and it does not use its own code. And yet it cannot be subclassed directly. In fact, the following code does not compile:
class Foo<E extends Enum<E>> extends Enum<E> { Foo(String name, int ordinal) { super(name, ordinal); } }
I know that Enum
is a special class in Java, and I understand that there are good reasons why direct subclassing is forbidden. But technically, how do you apply this behavior? Can a programmer create a similar non-finite class that would not allow a direct subclass, despite the presence of an accessible constructor?
source share