This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes S&B 1.5, Chapter 7, Question 2, answer C Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "S&B 1.5, Chapter 7, Question 2, answer C" Watch "S&B 1.5, Chapter 7, Question 2, answer C" New topic
Author

S&B 1.5, Chapter 7, Question 2, answer C

David Wooddall-Gainey
Greenhorn

Joined: Dec 13, 2007
Posts: 16

which can be inserted

A.List<List<Integer>> table = new List<List<Integer>>();
B.List<List<Integer>> table = new List<ArrayList<Integer>>();
C.List<List<Integer>> table = new ArrayList<ArrayList<Integer>>();
D.List<List,Integer>> table = new List<List,Integer>>();
E.List<List,Integer>> table = new ArrayList<List,Integer>>();
F.List<List,Integer>> table = new ArrayList<ArrayList,Integer>>();
G.none

Why is c wrong? Given explanation "the type argument <List<Integer>> must be the same on both sides of the assignment even though the constructor new ArrayList() on the right side is a subtype of the declared type List on the left side." Why does the type arg have to be the same?

Thanks
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
C. List<List<Integer>> table = new ArrayList<ArrayList<Integer>>();


Genrics doesn't allow you to write subclasses like that - meaning
you can't write :
List<Animal> l=new ArrayList<Dog>();

it should be
List<Dog> l=new ArrayList<Dog>();
List<Animal> l=new ArrayList<Animal>();

Both the types should be the same.

You cant even write
List<Number> n=new ArrayList<Integer>();

Hope you got the picture....


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

So does that mean only A is the correct answer right???


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Manju Kavi
Ranch Hand

Joined: Sep 25, 2008
Posts: 33
Yes Nabila is right. But here none of the option is right. Option "A" and "B" are wrong because List is an interface and you cannot call constructor on it. "C" is wrong according to what Nabila said, which is correct.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
If C was modified a little as
List<List<Integer>> table = new ArrayList<List<Integer>>();
it would have been correct.
David Wooddall-Gainey
Greenhorn

Joined: Dec 13, 2007
Posts: 16
Thanks and apologies to all. The correct answer is B which SHOULD read


(I am tying while riding on a bus to work!)

Nabila, do you mean since the left side of the assignment contains
List<List<Integer>> the base class is List while the generic class is List<Integer>. Base and Generic are terms from S&B Chap 7. If this is the case then, yes I understand you explanation. And thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: S&B 1.5, Chapter 7, Question 2, answer C
 
Similar Threads
Generic types
meaning of double angle brackets(<< >>) in Generics?
SCJP5 Study Guide Q2. Page 611/622 -- Generics and Collections
KB question pg 611
K&B generics doubt