Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Doubt

 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic