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;

}

}

[2309 byte] By [born2snipea] at [2007-11-26 18:57:19]
# 1

You haven't understand the wildcard,

this should compile:

private int totalWeight(List<? extends Base> base) {

return 0;

}

see java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf for more infos.

remi_foraxa at 2007-7-9 20:36:44 > top of Java-index,Core,Core APIs...