Consider the following (unchangable) API:
interface Bar {} class Foo { public static <T extends Foo & Bar> void doFoo(T param) { System.out.println("I shall be your ruler"); } } Now I have written a class that accepts a general Foo but in case that argument is also a Bar, I want to execute doFoo additionally in some method:
class Holder { public Holder(Foo foo) { this.foo = foo; } public void takeOverTheWorld() { if(this.foo instanceof Bar) { // Here I want to execute doFoo(), but I can't hand it over // because the bounds don't match // doFoo(this.foo); ) // Enstablish my reign } } Example usage of the Holder:
class Yes extends Foo implements Bar { } // ------ Holder h = new Holder(new Yes()); h.takeOverTheWorld(); // Should print "I shall be your ruler" As mentioned in the code comments, I have problems calling doFoo() in the Holder class, because no exact type is known at that time that does extend Foo and implement Bar, so I can't simply cast it to such a type. Is there a way to get around this without changing the interface of Holder, Foo and Bar?