I have two interfaces (IfaceA
, IfaceB
) and two classes implementing those interface (class C
, class D
):
interface IfaceA {
void doA();
}
interface IFaceB {
void doB();
}
class C implements IfaceA, IFaceB {
public void doA() {}
public void doB() {}
}
class D implements IfaceA, IFaceB {
public void doA() {}
public void doB() {}
}
I cannot change the signature of those classes.
How can I make a list or collection of instances of classes that implement both interfaces?
What I tried:
public static void main(String[] args) {
List<? extends IfaceA & IFaceB> test_1;
List<? extends IfaceA, IFaceB> test_2;
Class<? extends IfaceA, IFaceB>[] test_3;
}
are all wrong (a wildcard can have only one bound while I'm not sure whether it's possible with type bound).
I know this one might work:
Object[] objects = new Object[] {
new C(), new D()
};
for (Object o: objects) {
IfaceA a = (IfaceA) o;
IfaceB b = (IfaceB) o;
a.doA();
b.doB();
}
but this simply doesn't look right.