Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Richa Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Richa Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vijay but there are covariant return types in java
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
vijay saraf
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit
 
vijay saraf
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO...Ankit...i replied to Richa Only.
We both are saying the same thing.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic