I have a function that accepts a parameter that has some generic type
public < T extends SomeA > String foo(T t) {...}
Now... SomeA contains a series of methods that can be called, like baz1(), baz2(), baz3(), etc...
Now, here's the rub
The variable
Tthat will be passed intofoo(T t)will be an instance of eitherSomeBorSomeC, whereSomeB extends SomeAandSomeC extends someA.Both
SomeBandSomeCcontains a methodglop(...)which returnsSomeD, however,glop(...)is not inSomeA.I want to be able to pass
foo(T t)around so that it accepts eitherSomeBorSomeC, however, I need to callglop(...)(e.g follows)
E.g.
public < T extends SomeA > String foo(T t) {
return t.glop().methodInsideOfSomeD().toString();
}
I would rather not implement foo separately for SomeB and SomeC (i.e. fooSomeB fooSomeC). I want to be able to pass this method around to functions providing either SomeB or SomeC (or even better anything that contains the method glop(...)).
The problem: I do not have the ability to change the implementation or declaration of SomeB or SomeC since they reside in libraries I do not control. Thus, I cannot have them both implement some interface that provides glop(...).
Am I just out of luck?