wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic / Collections question 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 "Generic / Collections question" Watch "Generic / Collections question" New topic
Author

Generic / Collections question

David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30294
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

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
Sheriff

Joined: Sep 28, 2004
Posts: 18710
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic / Collections question