aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sierra/Bates, Chapter 7, Two Minute Section Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sierra/Bates, Chapter 7, Two Minute Section" Watch "Sierra/Bates, Chapter 7, Two Minute Section" New topic
Author

Sierra/Bates, Chapter 7, Two Minute Section

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Going through the summaries and two-minute section for each chapter. In chapter 7, it says the following:

An ArrayList<Animal> can accept references of type Dog, Cat, or any other subtype of Animal (subclass, or if Animal is an interface, implementation).


This is incorrect unless it is ArrayList <? extends Animal>. Is this a typo?

Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

No, it is totally correct. The thing is you can put a Dog or a Cat into ArrayList<Animal>, since both Dog and Cat IS-A Animal. However what you cannot do is to say ArrayList<Animal> list = new ArrayList<Dog>(); I hope that clears it out.


Personal page, SCJP 6 with 91%, SCWCD 5 with 84%, OCMJD
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Elchin Asgarli wrote:No, it is totally correct. The thing is you can put a Dog or a Cat into ArrayList<Animal>, since both Dog and Cat IS-A Animal. However what you cannot do is to say ArrayList<Animal> list = new ArrayList<Dog>(); I hope that clears it out.


Wouldn't this raise a compiler warning?

Is there sample code that demonstrates this?
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

Sandra Bachan wrote:
Elchin Asgarli wrote:No, it is totally correct. The thing is you can put a Dog or a Cat into ArrayList<Animal>, since both Dog and Cat IS-A Animal. However what you cannot do is to say ArrayList<Animal> list = new ArrayList<Dog>(); I hope that clears it out.


Wouldn't this raise a compiler warning?

Is there sample code that demonstrates this?


No, it wouldn't. Try following code:

Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3164
    
  47


If the reference type is ArrayList<Animal>, the generic type of the instantiation type must be Animal.So you can add any object, which extends Animal type to the list.

If the reference type is ArrayList<? extends Animal>, the generic type of the instantiation can be Animal, Dog, or Cat,So, since the compiler doesn't know the exact instantiation type, you cannot add anything, other than null, to the the list.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Aditya Kumar
Ranch Hand

Joined: Oct 31, 2006
Posts: 87
It's a strange coincidence but just yesterday I was going through the same section and I had the same question. But the answer given here is right and it puts things in perspective. I am glad that this thread happened.

Meanwhile, if you go to the same section (of the objective 6.4) in the book, you will see that they have given an example that could prove the two-minute drill statement right:



So this adds to the theory given at the end, that an ArrayList<Animal> can hold up objects of type Dog, Cat as they are the subclasses of Animal.

thanks for this, again.

- aditya
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Will try out the code
Thakur Sachin Singh
Ranch Hand

Joined: Jun 15, 2010
Posts: 232

hai answer given above is right...you take example which is given by book...it clear your doubt


SCJP 6- 91%, IBM DB2, IBM RAD Certified
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Tried it, thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sierra/Bates, Chapter 7, Two Minute Section