| Author |
Generics Doubt
|
Balaji Bang
Ranch Hand
Joined: Apr 23, 2007
Posts: 180
|
|
19)Which call(s) to method(addandDisp) are error free? From JavaBeat
a)only 3
b)only 1 and 3
c)only 1 and 4
d)only 1,2 and 3
e)only 1,3 and 4
Given Answer : e
My Doubt : public static <T> void addandDisp(Collection<T> cs, T t)
Here Collection is of type T and second parameter also T. So I thought only 4 is Correct.
How can we send this :
Her els3 of type Object means in the method addandDisp() first parameter is T holds Object , How come second Orameter T holds String???
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Her els3 of type Object means in the method addandDisp() first parameter is T holds Object , How come second Orameter T holds String???
A String IS-A Object.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Balaji Bang
Ranch Hand
Joined: Apr 23, 2007
Posts: 180
|
|
Explanation given is : Method call 2 cannot work,because the compiler cannot gurantee the type to
substitute.But in all other calls it is guranteed.
I didn't understand this ..... Please..
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Banu Chowdary wrote:Explanation given is : Method call 2 cannot work,because the compiler cannot gurantee the type to
substitute.But in all other calls it is guranteed.
I didn't understand this ..... Please..
With the second option, T is an unknown type that may extends Object. T may not be an Object type, as required by the second parameter.
Henry
|
 |
 |
|
|
subject: Generics Doubt
|
|
|