Casting a Generic List



Hello all,

Is it possible to cast an entire Generic List somehow? For example,
say I have the following:

public interface Foo {
.....
}

public class Bar implements Foo {
....
}

public class Test {
public List<Foo> getFooList() { .... }

public static void main(String[] args) {
Test test = new Test();

List<Bar> bars = test.getFooList(); // illegal
}
}

Now assume that all the objects in the FooList were indeed
instantiated as Bar objects.

I get a compile error saying it can't convert from List<Foo> to
List<Bar>. I could always just get a list of Foo objects and cast
them to Bars myself, but I'm just wondering if it can be done for me
all at once.

Any suggestions?

.



Relevant Pages