I have multiple interfaces and a class that implements all of them. I want to call all the methods defined in these interfaces on an instance of the implementing class, but I don't want to declare the variable as of the class type so that I'm still programming to interfaces.
It seems that I have either to:
- Cast the object from one interface type to another to call the appropriate methods.
- Create a union interface which extends the others and declare a variable of that type.
I would prefer not to do either of these because 1 is not type-safe and 2 can mean you quickly end up with a lot of 'union' interfaces which add nothing in themselves. Is it possible to have something like:
public interface A {
public void doThis();
}
public interface B {
public void doThat();
}
public class C implements A, B {
public void doThis(){;}
public void doThat(){;}
}
//Fake calling code:
public void go() {
A,B dualTypedInstance = new C(); //
dualTypedInstance.doThis();
dualTypedInstance.doThat();
}
It really seems like this should be possible. Thanks.