The following Java code compiles:
interface Interface1{}
interface Interface2{}
class MyClass1<T extends Interface1 & Interface2>{}
Also, the following code compiles:
class AClass{}
interface Interface3{}
class MyClass2<T extends AClass & Interface3>{}
The following code will give me a compile error:
interface Interface4 {}
class MyClass3<T, S extends T & Interface4> {}
So, as it seems, it is not possible to have multiple bounds if one of the bounding types is itself a type parameter. But why is that?
As I understand, if multiple bounds are declared, at most one of them may be a class (with all others being interfaces), and it must be the first one.
But, since in my example Interface
is an interface, why does the compiler complain? After all, it shouldn't make any difference whether T is an interface or a class.
I've tried to find an answer online, but apparently my googling skills are not sufficient. I'd love to understand what's going on here.