File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics problem 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 problem" Watch "Generics problem" New topic
Author

Generics problem

George Gates
Ranch Hand

Joined: Jul 11, 2008
Posts: 34
Hi All,

This question is from Javabeat 350 mock questions.

Question:
Which call(s) to method(addandDisp) are error free?


Options are:
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

Answer: e.

Can some one explain why the answer is 'e' and what is wrong with the other 4 options.

Thanks


Regards,<br />George Gates
Jamie MacDonald
Greenhorn

Joined: May 17, 2008
Posts: 20
Recheck your code. The variable s in the addandDisp() method is undeclared. In addition, the parameter t is unused.
Steve Ng
Greenhorn

Joined: Aug 10, 2008
Posts: 17
Hi,

I think that Jamie did not look carefully at the code. All the calls and declaration are fine.

The call 2 is wrong because:

the method arguments are as follows: addandDisp(Collection<T> cs, T t)

But the parameters passed in as follows: (List<? extends Object>, Object)

This results in ambiguity of types passed in because T is a "specific type and only 1 type" (for example T can be Integer) but ? extends Object can be anything that extends Object(such as Socket, Arrays etc). The compiler can see that this may be or may not be the type T so it isn;t safe to allow this to compile.

The call

List<? super Object> ls1 = new LinkedList<Object>();
addandDisp(ls1,new Object());

is safe because the compiler knows that T is super type of Object, so anything can be added to the collection because it's safe to passed in anything which can be cast to T(supertype)
[ August 11, 2008: Message edited by: Steve Ng ]
Jamie MacDonald
Greenhorn

Joined: May 17, 2008
Posts: 20
It is entirely possible that I am missing something. Can you tell me where the variable s in the 5th line of the code sample
s.add(o);
is declared?

I agree that not using the parameter t will not cause a problem. I just noted that as it seemed possible that it might point to an error in transcription.
Steve Ng
Greenhorn

Joined: Aug 10, 2008
Posts: 17
Originally posted by Jamie MacDonald:
It is entirely possible that I am missing something. Can you tell me where the variable s in the 5th line of the code sample
s.add(o);
is declared?

I agree that not using the parameter t will not cause a problem. I just noted that as it seemed possible that it might point to an error in transcription.


I believe what he meant is: cs.add(o);
George Gates
Ranch Hand

Joined: Jul 11, 2008
Posts: 34
Hi Steve,

I am sorry for the error. I did mean cs.add(o).
Thanks for explaining the solution.
The main concept is you cannot add anything in the collection that has wildcard '?' and extends. In other cases, adding is OK for the same type or any subtypes of the generic type.

Thanks again
kanishka weerasekara
Greenhorn

Joined: Aug 13, 2008
Posts: 4
in wild cards<? super someclass> will enable to add only the someclass objects no subclasses or super classes.
These wild cards are actually meant for assigning a reference to an actual collection object.
eg List<? extends Number> list=new ArrayList<Integer>();
list =new ArrayList<Integer>();
These wild cards are not meant for the adding object but for the assigned collection object.
Is my idea about this correct. I am doing my exam in two weeks time
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
hi kanishka, i think our issues are similar. please check this link
http://www.coderanch.com/t/269960/java-programmer-SCJP/certification/generic-super
but am not still clear with wat was explained there..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics problem