J
jagonzal
Hi,
I have an Interface B, which extends Interface A.
I have a method whose signature is:
public void doSomething(List<A> list){
....
}
However, when I try to invoke it like this:
List<B> list2 = new ArrayList<B>();
doSomething(list2);
the compiler complains that it can't take a List<B> instead of a
List<A>. I say that, theoretically, it should work, since a B element
is also a A element due to interface inheritance. Casting a B variable
to A does not produce a ClassCastException. However, the compiler says
that it doesn't.
Is there any way to go around this (Other than working with non-typed
lists)? (casting the list? I tried that but it doesn't work
TIA,
JGN.
I have an Interface B, which extends Interface A.
I have a method whose signature is:
public void doSomething(List<A> list){
....
}
However, when I try to invoke it like this:
List<B> list2 = new ArrayList<B>();
doSomething(list2);
the compiler complains that it can't take a List<B> instead of a
List<A>. I say that, theoretically, it should work, since a B element
is also a A element due to interface inheritance. Casting a B variable
to A does not produce a ClassCastException. However, the compiler says
that it doesn't.
Is there any way to go around this (Other than working with non-typed
lists)? (casting the list? I tried that but it doesn't work
TIA,
JGN.