Upcasting to generic list doesn't work?
Hi, I have two obects:
Aextends B
and two methods:
public List<A> getAObjects()
{
returnnew ArrayList<A>();
}
and
public List<B> getBObjects()
{
return getAObjects();
}
But I get errors in method "getBObjects()". How come? Surely I can do this cause Object A is an instance of object B.
any help much appreciated,
J
# 3
> That doesn't seem to work. But in my case B is an
> interface, and A is a class. Would that change things
> at all?
No, but your OP said "A extends B", which is not possible, if B is an interface and A a class.
For the rest: ibann is right. Assigning a List<A> to a List<? extends B> is definitely possible. You must have some other mistake in your code. Example:interface B { }
class A implements B { }
...
List<A> listA = new ArrayList<A>();
List<? extends B> listB = listA;