I have a class A and a class B extends A
In another class C I have a field
private List<B> listB;
Now, for some unusual reason, I have to implement this method in C
public List<A> getList();
I tried to do so by forcing an upcast of listB field to List<A>
via a List<?>
cast:
public List<A> getList(){
return (List<A>)(List<?>)listB;
}
Clients should do
List<A> list = getList();
for(A a:list){
//do something with a
}
I did some test and it seems work correctly, but honestly I am not sure of the all possible implications.
Is this solution correct? And Is it the best solution?
Thanks for your answers.