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

[728 byte] By [justinlawlera] at [2007-11-27 9:31:07]
# 1

Hi,

List<A> is not a "child" of List<B>.

A is a "child" of B.

This is different.

But, you can do this:

public List<? extends B> getBObjects(){

return getAObjects();

}

ibanna at 2007-7-12 22:45:08 > top of Java-index,Core,Core APIs...
# 2
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?thanks again
justinlawlera at 2007-7-12 22:45:08 > top of Java-index,Core,Core APIs...
# 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;

stefan.schulza at 2007-7-12 22:45:08 > top of Java-index,Core,Core APIs...