I have a family of domain models, each of which has a subclass that extends it and implements a specific interface, like this (Cloneable
is not the interface in question, it's for example purposes only):
class A{}
class B extends A implements Cloneable{}
class C{}
class D extends C implements Cloneable{}
I want to create a generic type signature that will enforce this pairing, I've tried this which FAILS:
<T1,T2 extends T1 & Cloneable> void f ( T1 t1, T2 t2 ){}
but I get message in my IntelliJ IDE "Type parameter cannot be followed by other bounds"
; it still FAILS if I switch the order around to:
<T1,T2 extends Cloneable & T1> void f ( T1 t1, T2 t2 ){}
I get the message "Interface expected here."
Confusingly, both of these signatures WORK:
<T extends A & Cloneable> void f( A a, T t ){}
<T1,T2 extends T1> void f ( T1 t1, T2 t2 ){}
Is this just a weird limitation of Java's generic type system that I cannot have a generic class (ie T2
) extend both another generic class (ie T1
) and a concrete interface (eg Cloneable
)?
tl;dr: So, why won't <T1,T2 extends Cloneable & T1> void f ( T1 t1, T2 t2 ){}
compile: is it a limitation of the Java generic syntax or am I using the wrong syntax?