aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bates & Sierra question #8  generics return types 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 "Bates & Sierra question #8  generics return types" Watch "Bates & Sierra question #8  generics return types" New topic
Author

Bates & Sierra question #8 generics return types

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
question #8 in the book is:



A programmer wants to use the method like this:





I understand that it is ok to pass in an ArrayList<Integer> as a method argument, but why can't it return an ArrayList<Integer> ? For example..

ArrayList<Integer> input = null; // this is ok
ArrayList<Integer> output = null; // this is not ok ???

vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
Rachel Glenn wrote:



why can't it return an ArrayList<Integer> ? For example..

ArrayList<Integer> input = null; // this is ok
ArrayList<Integer> output = null; // this is not ok ???



Same doubt here....

please explain if sub-type is a legal return type why it is necessary to declare return type as a List and why not ArrayList ?? please explain...
Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

The question isn't really anything to do with generics. The method returns a List; but ArrayList is a subtype of that. So assigning the result of the method to an ArrayList would be a down-cast.

You said:

why can't it return an ArrayList<Integer> ?


Well, it might. But all we (and the compiler) know is that it returns something which implements List<Integer>.
vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
Question no 8 chapter 7 in the book of Kathy Sierra and Bert Bates

Question is -:

Given a method declared as-:



A programmer wants to use this method like this-:



Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow
the code to compile? (Choose all that apply.)
A.
B.
C.
D.
E.
F.
G. None

Correct answer is B, E and F


I am still not getting why correct answer is only B, E and F...Why not A. Please explain this...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

It can't be A because this is an entirely valid implementation of process.


Which means in this case the method is not returning an ArrayList. But you're trying to assign it to an ArrayList variable. That's not safe, so the compiler won't allow it.
vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
Matthew Brown wrote:It can't be A because this is an entirely valid implementation of process.


Which means in this case the method is not returning an ArrayList. But you're trying to assign it to an ArrayList variable. That's not safe, so the compiler won't allow it.


Thank You
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
OK, it seems the 'rules' for input generic parameters are different than for generic return parameters. But as I understand it, the methods contract in this problem is:




The method says it is returning a List<E> reference , and while in reality the method could be returning any subtype of List<E>, the calling function can not make any assumptions about the actual implementation of the return type (Arraylist or any subtype of List).

 
 
subject: Bates & Sierra question #8 generics return types