Are these two (valid) generic bounds:
<T extends Enum<T> & MyInterface>
<T extends Enum<? extends MyInterface>>
the same?
Suppose I have an interface
interface MyInterface {
void someMethod();
}
And some enums that implement it:
enum MyEnumA implements MyInterface {
A, B, C;
public void someMethod() {}
}
enum MyEnumB implements MyInterface {
X, Y, Z;
public void someMethod() {}
}
And I want to require that an implementation uses not only a MyInterface
but also that it is an enum. The "standard" way is by an intersection bound:
static class MyIntersectionClass<T extends Enum<T> & MyInterface> {
void use(T t) {}
}
But I've discovered that this also works:
static class MyWildcardClass<T extends Enum<? extends MyInterface>> {
void use(T t) {}
}
With the above, this compiles:
public static void main(String[] args) throws Exception {
MyIntersectionClass<MyEnumA> a = new MyIntersectionClass<MyEnumA>();
a.use(MyEnumA.A);
MyWildcardClass<MyEnumB> b = new MyWildcardClass<MyEnumB>();
b.use(MyEnumB.X);
}
And the bound works as and intended and required by above for both cases.
Is there a difference between these two bounds, if so what, and is one "better" than the other?