• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • 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 ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi

Barry Gaunt

Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic