aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics and Collections 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 "Generics and Collections" Watch "Generics and Collections" New topic
Author

Generics and Collections

shashank dwivedi
Ranch Hand

Joined: Mar 06, 2012
Posts: 61
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

Joined: Feb 09, 2012
Posts: 583

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?


Tell the difficulties that i am difficult.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Jul 09, 2001
Posts: 1179
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


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
shashank dwivedi
Ranch Hand

Joined: Mar 06, 2012
Posts: 61
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

Joined: Feb 09, 2012
Posts: 583

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

Joined: Mar 06, 2012
Posts: 61
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

Joined: Mar 06, 2012
Posts: 61
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

Joined: Feb 09, 2012
Posts: 583

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

Joined: Jun 02, 2012
Posts: 186
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

Joined: Mar 06, 2012
Posts: 61
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

Joined: Jul 09, 2001
Posts: 1179
Good Day,

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




The error is -



Regards,
Dan
shashank dwivedi
Ranch Hand

Joined: Mar 06, 2012
Posts: 61
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
 
subject: Generics and Collections