Generics again

Discussion in 'Java' started by Sierra Bravo, Aug 2, 2005.

  1. Sierra Bravo

    Sierra Bravo Guest

    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();
    }
    }
     
    Sierra Bravo, Aug 2, 2005
    #1
    1. Advertising

  2. Sierra Bravo

    Roedy Green Guest

    On 1 Aug 2005 21:53:07 -0700, "Sierra Bravo" <>
    wrote or quoted :

    > * what is "capture of ? extends Person"?
    > * what is the functional difference between the following two usages?


    see the thread "generics error messages".

    Chris just went over this.

    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Aug 2, 2005
    #2
    1. Advertising

  3. Sierra Bravo

    Chris Smith Guest

    Sierra Bravo <> wrote:
    > 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);
    > -----------


    You have a reference of type List<? extends Person>. From the
    compiler's standpoint, that could point to an object of class
    List<Person>, or perhaps List<Programmer>, or perhaps
    List<SportsPerson>. It doesn't really know which. So, when you try to
    add a Programmer to the list (for example), it isn't safe... because the
    list MIGHT be a List<SportsPerson>, in which case adding the programmer
    is an error. The type of the reference is the ONLY place the compiler
    looks to do this kind of reasoning, and the wildcard type does not
    guarantee that the operation is safe.

    For this reason, in order to invoke add on a List, you need to have a
    lower bound, either by specifying a specific type for the list
    (List<Person> instead of List<? extends Person>) or by specifying a
    lower bound explicitly (List<? super Programmer>, for example).

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Aug 2, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. amit
    Replies:
    0
    Views:
    394
  2. Juergen Berchtel
    Replies:
    1
    Views:
    6,008
    John C. Bollinger
    May 20, 2005
  3. che
    Replies:
    2
    Views:
    501
  4. Roedy Green

    Generics Warnings yet again

    Roedy Green, Jul 27, 2008, in forum: Java
    Replies:
    3
    Views:
    341
    Roedy Green
    Jul 27, 2008
  5. Soul
    Replies:
    0
    Views:
    524
Loading...

Share This Page