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

Generics - Chapter 7 Question 8 page 653

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Given a method declared as

A programmer wants to use the method like this:

Which pair of declarations could be placed at //INSERT CODE HERE to allow the code to compile?

I understand why A, B, C and G are incorrect.

I understand why E and F are correct.

I do not understand why using D is correct.

D:

Why is it ok for the method to declare that it will accept a list - and the accept an ArrayList
When it is not ok for the return type to declare as a List but not return an ArrayList?

thanks


OCPJP 6, OCMJD
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

Glen Iris wrote:I do not understand why using D is correct.

Well, I've not seen the other options yet, but as per code, below things are clear:
1) method accepts List of type E
2) method returns List of type E
3) E must be IS-A Number

Option D satisfies all these requirements.

Glen Iris wrote:Why is it ok for the method to declare that it will accept a list - and the accept an ArrayList

because, ArrayList IS-A List.
Glen Iris wrote:When it is not ok for the return type to declare as a List but not return an ArrayList?

if return type is List, method must return something which IS-A list.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
List is an interface implemented by ArrayList. ArrayList is a List.
Therefore, you can pass an ArrayList as the argument. List<Integer> list = new ArrayList<Integer>() ; // compiles.
I hope this can help answer your question.
 
 
subject: Generics - Chapter 7 Question 8 page 653