Generic type List
IfA andB are both instances ofBase why can't I just loop over a LIst<Base>?
I am receiving a compiler error when I attempt to callgetWeight() on classC. The method totalWeight does not like me passing in my lists (as or bs).
import java.util.List;
import java.util.ArrayList;
interface Base{
publicint getWeight();
}
class Aimplements Base{
publicint getWeight(){
return 0;
}
}
class Bimplements Base{
publicint getWeight(){
return 0;
}
}
class Cimplements Base{
private List<A> as =new ArrayList<A>();
private List<B> bs =new ArrayList<B>();
publicint getWeight(){
int total = totalWeight(as);
total += totalWeight(bs);
return total;
}
privateint totalWeight(List<Base> base){
return 0;
}
}

