Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generics again

Reply
Thread Tools

Generics again

 
 
Sierra Bravo
Guest
Posts: n/a
 
      08-02-2005
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();
}
}

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-02-2005
On 1 Aug 2005 21:53:07 -0700, "Sierra Bravo" <(E-Mail Removed)>
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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      08-02-2005
Sierra Bravo <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
Generics Warnings yet again Roedy Green Java 3 07-27-2008 09:22 AM
HttpSession gets generated again and again!! PLEASE HELP ME!!!! che Java 2 10-10-2005 10:20 PM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
jserve booting again and again amit Java 0 10-02-2003 04:26 PM



Advertisments