Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics and Collections

 
shashank dwivedi
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K&B SCJP6 p.no 653


Answers are: B,E,F
Explanations given: return type of process is definitely as a List, not ArrayList so A and D are not correct.
C is wrong because return type evaluates to List<Integer> , and that can't be assigned to variable of type List<Number>.

Doubts: A should also be incorrect as method takes List not ArrayList. why is it not so??
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you have forgotten basic rule of OOP.ArrayList is a class which implements List interface, now i want ask to you why List reference variable can't refer ArrayList Class object?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what you have is a generic method declaration which takes a list parameterized as E extends Number and returns a list parameterized to E extends Number. you would be familiar with the notation E extends Number. it means the method can take any list containing Number or anything that extends Number. now comes the main part. the return type of the method is List<E> which means you can return anything which IS-A list. for eg. if i have Animal class and a Dog class which extends Animal class. then if i have a method which returns Animal, then in that method i can return Dog. if you want to dig deeper you should refer JLS.( have to dig through my bookmarks so will post the link later). basically the return type of the methods go through assignment conversion mentioned in the jls. so you can return anything from a method which is "assignable"(according to the rules given in assignment conversion section of jls) to the return type declared. in your case the return type is List<E> so that you can return any List parameterized to E . i.e you can return ArrayList, LinkedList.

let us consider option A. let us say you return ArrayList<Integer> in your method. this is perfectly fine since it is "assignable" and the parameter is also a subtype of Number class. the type of the list object created here is ArrayList<Integer> , no doubt about that, but the return type is List<Integer> which means you CANNOT assign the return type of process method to ArrayList<Integer> reference variable. as said by Saloni a supeclass reference can refer child object but not vice-versa. here you are violating this principle. so although the actual object created is of type ArrayList<Integer> but since return type is List<Integer> , the OUTPUT reference variable should be of the type returned by the method or its supertype.

the links i was talking about are as follows.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.17

http://www.coderanch.com/t/579116/java-programmer-...ation/primitive-casting-boxing
 
Dan Drillich
Ranch Hand
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shashank dwivedi wrote:
Doubts: A should also be incorrect as method takes List not ArrayList. why is it not so??


You are right - A is incorrect.

Regards,
Dan
 
shashank dwivedi
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Drillich wrote:
shashank dwivedi wrote:
Doubts: A should also be incorrect as method takes List not ArrayList. why is it not so??


You are right - A is incorrect.

Regards,
Dan

I am sorry for i mistaken.
I mean why B is correct ? Method is defined to take List and not ArrayList.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange that you didn't read previous posts, still they could help you.

Okay,I'll still repeat same thing here.

ArrayList is a class which implements List interface, now i want ask to you why List reference variable can't refer ArrayList Class object?

 
shashank dwivedi
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:Strange that you didn't read previous posts, still they could help you.

Okay,I'll still repeat same thing here.

ArrayList is a class which implements List interface, now i want ask to you why List reference variable can't refer ArrayList Class object?


Actually i am confused because I read in K&B that we can't pass polymorphic types in Generics as Arrays.
else why should options A and D are incorrect?
 
shashank dwivedi
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shashank dwivedi wrote:
saloni jhanwar wrote:Strange that you didn't read previous posts, still they could help you.

Okay,I'll still repeat same thing here.

ArrayList is a class which implements List interface, now i want ask to you why List reference variable can't refer ArrayList Class object?


Actually i am confused because I read in K&B that we can't pass polymorphic types in Generics as Arrays.
else why should options A and D are incorrect on basis of return types?

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got your problem, you're mixing here Generic Type Declaration with Reference Variable Declaration,both are different thing.

You can see here Generic Type is always <Integer> not like List<Number> mylist=new ArrayList<Integer>(); that will be always wrong. but List<Integer> mylist=new ArrayList<Integer>(); why it should be wrong ??

Again see closely below code and know difference.

 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Drillich wrote:You are right - A is incorrect.

A is incorrect because,
-we require return type as List<E> i.e. List of E type variables.
-And List<E> can refer to any objects of type ArrayList<E>, Vector<E> and LinkedList<E>
-So, with A option, we are trying to say that return type of the object is only of type ArrayList<E>...Which is definitely false.

shashank dwivedi wrote:I mean why B is correct ? Method is defined to take List and not ArrayList.

B is correct because
-It is the reverse of A.
-Argument type is List<E> so it could be of type ArrayList<E>, Vector<E> and LinkedList<E>. (As List is implemented by all....i.e. Superclass )
-With B option, we are trying to say that return type of the object is any type of List<E>...Which is definitely correct.

For more Information:
 
shashank dwivedi
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurangkumar Khalasi wrote:
Dan Drillich wrote:You are right - A is incorrect.

A is incorrect because,
-we require return type as List<E> i.e. List of E type variables.
-And List<E> can refer to any objects of type ArrayList<E>, Vector<E> and LinkedList<E>
-So, with A option, we are trying to say that return type of the object is only of type ArrayList<E>...Which is definitely false.

shashank dwivedi wrote:I mean why B is correct ? Method is defined to take List and not ArrayList.

B is correct because
-It is the reverse of A.
-Argument type is List<E> so it could be of type ArrayList<E>, Vector<E> and LinkedList<E>. (As List is implemented by all....i.e. Superclass )
-With B option, we are trying to say that return type of the object is any type of List<E>...Which is definitely correct.

For more Information:

thanks all I got it.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day,

The following example helped me to be sure about why A is wrong -




The error is -



Regards,
Dan
 
shashank dwivedi
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Drillich wrote:Good Day,

The following example helped me to be sure about why A is wrong -




The error is -



Regards,
Dan

thanks Dan. It helped me a lot
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic