I have an abstract class A<T>
with generic wildcard T. There are two extensions of A,
public class A1 extends A<Integer> {...}
and
public class A2 extends A<String> {...}
Now I have another class, let's call it B, that is basically a collection of either A1 or A2. So I defined it as a class of generic type T extends A:
public class B<T extends A> {...}
What I would like to be able to do is, within B, create methods that return the type of T's generic. For example,
B<A1> foo = new B<A1>;
foo.get(); // returns Integer, corresponding to A1's generic type
and
B<A2> bar = new B<A2>;
bar.get(); // returns String, corresponding to A2's generic type
Is it possible to do this in Java? I'm having trouble figuring out what return type to put when declaring B's methods (if I wanted B to return T, I'd put public T get() {...}
, but I actually want to return the parametrized type of T). Or is there a pattern that solves this problem better than the way I'm approaching it?
Thanks.