My project uses an external library which provides classes A
and B
(where B extends A
). I want to add a few methods to A
, but I want them to be in B
too, so I have created an interface Custom
and created the classes:
CustomA extends A implements Custom
CustomB extends B implements Custom
I now want to uses those 2 new classes interchangeably in class C
, as both attributes and method arguments. I have found this question: Declare an attribute that both extends a class and implements an interface, so I have tried the following code:
public class C<T extends A & Custom>{
private T attr1;
private T attr2;
public void createAttrs() {
attr1 = new CustomA();
attr2 = new CustomB();
}
public void setAttr1(T attr1) {
this.attr1 = attr1;
}
public void setAttr2(T attr2) {
this.attr2 = attr2;
}
}
However this code did not compile because of "Incompatible types" in the createAttrs()
method. But CustomA
and CustomB
both extend A
(directly or indirectly) and implement Custom
, so it matches the pattern of the Type Variable T
.
How can I make this code work?
Edit: What I'm really looking for is a way to use CustomA
and CustomB
interchangeably, in the same fashion I could originally store both an A
and a B
object in a variable typed A
.