aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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

Joined: Feb 28, 2007
Posts: 1707


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 ]

cmbhatt
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Barry Gaunt

Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics