S
Sierra Bravo
Given that "Programmer" and "SportsPerson" are both derived from
"Person", why doesn't the following fragment compile? The error is:
------------
Gen.java:50: cannot find symbol
symbol : method add(Person)
location: interface java.util.List<capture of ? extends Person>
someguy.add(p1);
-----------
In particular,
* what is "capture of ? extends Person"?
* what is the functional difference between the following two usages?
List<? extends Person> someguy = new ArrayList<Programmer>();
List<Programmer> someguy = new ArrayList<Programmer>();
TIA
sb
----------------------------------------------
public class Gen {
List<Person> anyguy = new ArrayList<Person>();
List<? extends Person> someguy = new ArrayList<Programmer>();
Person p1,p2,p3;
public void go() {
p1 = new Programmer("Ritchie");
p2 = new Programmer("Thompson");
p3 = new SportsPerson("Williams");
anyguy.add(p1);
anyguy.add(p2);
anyguy.add(p3);
someguy.add(p1);
someguy.add(p2);
someguy.add(p3);
}
public static void main(String s[]) {
(new Gen()).go();
}
}
"Person", why doesn't the following fragment compile? The error is:
------------
Gen.java:50: cannot find symbol
symbol : method add(Person)
location: interface java.util.List<capture of ? extends Person>
someguy.add(p1);
-----------
In particular,
* what is "capture of ? extends Person"?
* what is the functional difference between the following two usages?
List<? extends Person> someguy = new ArrayList<Programmer>();
List<Programmer> someguy = new ArrayList<Programmer>();
TIA
sb
----------------------------------------------
public class Gen {
List<Person> anyguy = new ArrayList<Person>();
List<? extends Person> someguy = new ArrayList<Programmer>();
Person p1,p2,p3;
public void go() {
p1 = new Programmer("Ritchie");
p2 = new Programmer("Thompson");
p3 = new SportsPerson("Williams");
anyguy.add(p1);
anyguy.add(p2);
anyguy.add(p3);
someguy.add(p1);
someguy.add(p2);
someguy.add(p3);
}
public static void main(String s[]) {
(new Gen()).go();
}
}