This is an academic question intended to better understand generics behave in this scenario.
The Enum
type is described as "the common base class of all Java language enumeration types". Its definition is:
public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable {
}
Now, I could define a field with this definition:
public class MyClass {
public Enum<MyEnum> field;
}
All fine. Now, take it one step further. Since Enum<MyEnum>
is apparently equivalent to MyEnum
that would leave me to believe that according to the definition I also could write:
public class MyClass {
public Enum<Enum<MyEnum>> field;
}
But now the compiler actually stops me: Type parameter 'java.lang.Enum' is not within its bound; should extend 'java.lang.Enum<MyEnum>>
which it does according to the definition since it still satisfies: E extends Enum<E>
. Of course, there are all kinds of reasons why you would not allow such recurring (or perhaps better, incurring) construction, but the one given does feel wrong to me.
On the other hand, if I were to define a class like this (in analogue to the definition of the Enum
)
public class Wrapper<T extends Number> {
}
I would be able to write a class:
public class MyClass {
// satisfies the upper bound
public Wrapper<Integer> field1;
// also satisfies the upper bound
public Wrapper<Number> field2;
}
Am I overlooking something or is my analysis misguided?