File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bates & Sierra question #8 generics return types

 
Rachel Glenn
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic