aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Doubt" Watch "Generics Doubt" New topic
Author

Generics Doubt

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 182

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: 19073
    
  40

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: 182

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: 19073
    
  40

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