class Father{}Now you're going to do like:
class Son extends Father{}
void doSomething(List<Father> list){}This won't work because you can't cast a generic type of one parameter to another. Java will give to you compile error.
doSomething(new ArrayList<Son>());
Solution is to use wildcard type:
void doSomething(List<?> list){
List<Parent> parents = (List<Parent>)list;
}
No comments:
Post a Comment