• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sierra/Bates, Chapter 7, Two Minute Section

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Elchin Asgarli
Ranch Hand
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4600
305
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Aditya Kumar
Ranch Hand
Posts: 102
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will try out the code
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai answer given above is right...you take example which is given by book...it clear your doubt
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried it, thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic