• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Generics

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class TestWildcards {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<Integer>();
Bar bar = new Bar();
bar.doInsert(myList);
}
}

class Bar {
void doInsert(List<Object> list) {
list.add(new Dog());
}
}


Now will it work? If not, why not?
This time, class Bar, with the doInsert() method, compiles just fine. The
-------------------------------------------------- -----------------
problem is that the TestWildcards code is trying to pass a List<Integer> into a
method that can take ONLY a List<Object>. And nothing else can be substituted for
<Object>.

These statements are from K&B ,see above underline words.
I think the program will not compile.

Thanks
Anil kumar
[ April 26, 2007: Message edited by: anil kumar ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Wont compile!
Try to pass a List parameterized with Integer to a method which only asks
for List parameterized with Object nothing else.


You can make it:




Regards,
cmbhatt
[ April 26, 2007: Message edited by: Chandra Bhatt ]
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Chandra
Thanks
ya it will not work.

But i have seen and i think it is not included in errata.My be for them it is a minor mistake.

Thanks
Anil kumar
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This time, class Bar, with the doInsert() method, compiles just fine.



Why won't class Bar compile fine?

It tells you why class TestWildcards does not compile - not class Bar.
[ April 26, 2007: Message edited by: Barry Gaunt ]
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Barry Gaunt

Thank you.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic