• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Generics problem

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recheck your code. The variable s in the addandDisp() method is undeclared. In addition, the parameter t is unused.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi kanishka, i think our issues are similar. please check this link
https://coderanch.com/t/269960/java-programmer-SCJP/certification/generic-super
but am not still clear with wat was explained there..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic