• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic / Collections question

 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Opera Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings everyone

I keep reading same pages over and over, yet I do not understand a small detail which is a little confuse.

Given this scenario :


And based in these words:

In other words, a method that takes, say, an ArrayList<Animal> will NOT be
able to accept a collection of any Animal subtype! That means ArrayList<Dog>
cannot be passed into a method with an argument of ArrayList<Animal>, even
though we already know that this works just fine with plain old arrays.
Obviously this difference between arrays and ArrayList is consistent with the
polymorphism assignment rules we already looked at—the fact that you cannot
assign an object of type ArrayList<JButton> to a List<Object>. But this is where
you really start to feel the pain of the distinction between typed arrays and typed
collections.


Fine , the declared type argument is Animal, for addAnimal method , not any other, so there's no point passing subtypes .

Then, when it comes to add method (List interface), why this is fine "some times" :



Is it really fine? Isn't suppose JVM it's expecting just a List of Animal class type only?

Wouldn't be better simply to change previous code into : (using wildcard )



The list is supposed to hold Animal type objects, right? So, in this case if you try to add a Dog type object (subtype of Animal class) , shouldn't be wrong? What am I missing?


Thanks in advance ^_^
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34860
369
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point the book is making is that you can add a Dog to a list of Animals. But only ArrayList<Animal> may be passed into the method. ArrayList<Dog> may not. Nor may ArrayList<Cat>.

Read about super vs extends. Extends won't help in this example because then I could pass in ArrayList<Cat>. The compiler is smart enough to not allow adding a dog with this signature.

ps - it's helpful to cite sources of questions when quoting. I'll do so here - page 587-589 of K&B SCJP 5.
 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Opera Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:The point the book is making is that you can add a Dog to a list of Animals. But only ArrayList<Animal> may be passed into the method. ArrayList<Dog> may not. Nor may ArrayList<Cat>.

Read about super vs extends. Extends won't help in this example because then I could pass in ArrayList<Cat>. The compiler is smart enough to not allow adding a dog with this signature.

ps - it's helpful to cite sources of questions when quoting. I'll do so here - page 587-589 of K&B SCJP 5.


Thank you Jeanne , you are right, let's give useful information for rest needing to know it . Sun Certified Programmer for Java 6 Study Guide from Kathy Sierra and Bert Bates, Chapter 7 - Generics, Pages from 607 to 622.

=====

With this said, I am going to take advantage for ask a doubt concerning test question ( It's related with the following topic as well -> http://www.coderanch.com/t/261444/java-programmer-SCJP/certification/super-wildcard )

This is the question test: (Same book, page 653 , inside chapter 7, question number 8 )


C says to be wrong because return type evaluates List <Integer> . This is the part I don't get. It's supposed to return a List <Number> or List <Integer> , in order words, it returns a List of Number type or subtypes of this one. Then, why is it excluding Number and only taking care about subtye? . I didn't know it was excluding.

With this say, does it work in the same way with super?

Thanks in advance.
 
Henry Wong
author
Marshal
Pie
Posts: 21423
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Samer wrote:


C says to be wrong because return type evaluates List <Integer> . This is the part I don't get. It's supposed to return a List <Number> or List <Integer> , in order words, it returns a List of Number type or subtypes of this one. Then, why is it excluding Number and only taking care about subtye? . I didn't know it was excluding.

With this say, does it work in the same way with super?





Simply, the type to be returned is "List<E>", as defined -- and since the nums parameter passed in IS-A List<Integer>, the generic E type can only be an Integer. So, the type returned must be a List<Integer>.

Henry
 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Opera Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:




Simply, the type to be returned is "List<E>", as defined -- and since the nums parameter passed in IS-A List<Integer>, the generic E type can only be an Integer. So, the type returned must be a List<Integer>.

Henry


I have understood it finally.

My bad,I was confuse due I was somehow mixing up both Generic argument type of method before the return type and the return type of the list returned from method. (I guess it's part of the tricky question).

As easy as you have stated Henry , I was able to see it better thanks to your answer, therefore , thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic