Suppose that I have the following class:
public class Either<A, B> {
public Object get();
}
Either
is a type that stores one object of either type A or B. get()
retrieves that one object.
The question is whether or not it is possible to use generics to alter the method signature of get()
so that the type returned is not just Object
, but any common supertype of A and B. For example, an Either<Integer, Long>
can have get()
return Number
, an Either<Deque<T>, Set<T>>
can have get()
return Iterable<T>
or Collection<T>
, and so on. (Obviously, an Either<Foo,Foo>
should have get()
return Foo
).
If this is at all possible, if I had Either<List<A>, List<B>>
, what is the most specific type get()
can return? Is it raw List
, wildcard List<?>
, or something else entirely?
, List>`? Anyway it would need to be `List>`.
– Paul Bellora Sep 04 '12 at 03:19