It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question8 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 "Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question8" Watch "Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question8" New topic
Author

Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question8

Richa Sharma
Ranch Hand

Joined: Dec 06, 2008
Posts: 47
I have a doubt in Q.8 Chapter-Generics and Collections of Kathy Sierrra book for SCJP 6.

Question:

Given a method declared as:


Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow
the code to compile? (Choose all that apply.)

A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;

B. ArrayList<Integer> input = null;
List<Integer> output = null;

C. ArrayList<Integer> input = null;
List<Number> output = null;

D. List<Number> input = null;
ArrayList<Integer> output = null;

E. List<Number> input = null;
List<Number> output = null;

F. List<Integer> input = null;
List<Integer> output = null;

G. None of the above.

The correct answers are B,E,F

My doubt is why is choice A incorrect?
vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
as per the return type the method would return a List so the 'output' variable must be a List.
How will you assign the List to ArrayList ?


Thanks
Vijay Saraf.
Richa Sharma
Ranch Hand

Joined: Dec 06, 2008
Posts: 47
Thanks Vijay but there are covariant return types in java
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Hint:

Can you do this

ArrayList list = new Object();

or this

String str = new Object();

Thats what the option A is trying to do i.e. assign a List into an ArrayList...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
It doesn't mean that you can assign a object to a subtype without cast
see the code



This code won't comiple...

as method1() is returnig a String ( Sub class object of the Object Class) in actual,but method signature says it will return a Object type object
so returning value can't be assigned to Sub class object without downcast.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

vijay saraf wrote:It doesn't mean that you can assign a object to a subtype without cast


Was this for me?? Well if yes, then I was just trying to say that direct assignment of a super type object to a sub-type reference will result in a compilation error. The code of option A will fail because the return type of the method is List as opposed to the reference in which it is assigned. Just merge the code and the option and everything will be crystal clear



Even if we forget generics, option A wont compile

Richa Sharma
Ranch Hand

Joined: Dec 06, 2008
Posts: 47
Thanks Ankit
vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
NO...Ankit...i replied to Richa Only.
We both are saying the same thing.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

vijay saraf wrote:NO...Ankit...i replied to Richa Only.
We both are saying the same thing.


Yes we are saying the same thing. That's why I was confused that why are you telling this to me ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question8