File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics + Polymorphism 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 "Generics + Polymorphism" Watch "Generics + Polymorphism" New topic
Author

Generics + Polymorphism

Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

Hi all,

Another question about generics.
I don't understant why, cant add an instance of Object into the list (List<? super String>) as follows at code below:



Could anyone help me to understand it?

thanks.


Adolfo Eloy
Software Developer
OCPJP 6
Jim Jonides
Ranch Hand

Joined: Oct 12, 2010
Posts: 42
My understand is that you can only put subtype of E to the list if the list is declared as List<? super E>.


Devil is in the detail.
Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

Adolfo Eloy wrote:Hi all,

Another question about generics.
I don't understant why, cant add an instance of Object into the list (List<? super String>) as follows at code below:



Could anyone help me to understand it?

thanks.


Think of it this way:
List<? super String> means that the instantiation can be done with String or any supertype of String. We do however not know which supertype.
So when you try to add to the list the compiler wont know what the generic type of the list will be.

Consider the following example:


The key point to remember is that List<? super Cat> does not mean you can add Cat or any supertype of Cat. It means that the generic type will be Cat or any supertype of Cat. When you think about it that way it should be clear why you can only add Cats or subtypes of Cat (in your case String or subtypes of String (which of course will be quite difficult )).

Hope that makes it clearer?


OCPJP (310-065) - 88%
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Malte Wannerskog wrote:

The key point to remember is that List<? super Cat> does not mean you can add Cat or any supertype of Cat. It means that the generic type will be Cat or any supertype of Cat. When you think about it that way it should be clear why you can only add Cats or subtypes of Cat (in your case String or subtypes of String (which of course will be quite difficult )).



Please show an example of adding the generic type of Cat or any supertype of Cat. I'm confused too!


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

Sandra Bachan wrote:
Malte Wannerskog wrote:

The key point to remember is that List<? super Cat> does not mean you can add Cat or any supertype of Cat. It means that the generic type will be Cat or any supertype of Cat. When you think about it that way it should be clear why you can only add Cats or subtypes of Cat (in your case String or subtypes of String (which of course will be quite difficult )).



Please show an example of adding the generic type of Cat or any supertype of Cat. I'm confused too!


You can only add Cats or subtypes of Cat, not supertypes.
The way you should think when you see <? super Cat> is that the generic type of the list could be Cat or any supertype of Cat:

All four options above are possible, the compiler does not know which generic type the list is, only that it's Cat or supertype of Cat.
Now what is true for list1,list2,list3 and list4? Which type of objects does the compiler know for certain that we can add to all 4 lists...?

... Only Cats and Kittens since the following holds true:
Cat Is-A(n) Animal
Cat Is-A Life
Cat Is-A(n) Object
Kitten Is-A Cat (and therefore also Is-A(n) Animal, Life and Object).

If you saw

The compiler would not allow you inserting Life into the list since
Life Is-A(n) Animal does not hold true.

If the compiler knew that the generic type was animal it would allow Animal, Cat or Kitten.
But when you type <? super Cat> it doesnt know wether the generic type will be Cat, Animal, Life or Object.
Therefore only Cats and Kittens are safe to add.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Malte Wannerskog wrote:
The way you should think when you see <? super Cat> is that the generic type of the list could be Cat or any supertype of Cat:
.


If I understand correctly you can add Cat to list, Animal to list2, Life to list3, and Object to list4?
Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

Sandra Bachan wrote:
Malte Wannerskog wrote:
The way you should think when you see <? super Cat> is that the generic type of the list could be Cat or any supertype of Cat:
.


If I understand correctly you can add Cat to list, Animal to list2, Life to list3, and Object to list4?


No that is not the case at all.

The compiler only knows that list,list2, list3 and list4 are of type List<? Super Cat> and therefore doesnt know if you have chosen Cat, Animal, Life or Object as the generic type.
That means you can only add objects that are subtypes of both Cat, Animal, Life and Object to list, list2, list3 and list4. In this case... Cats and Kittens!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics + Polymorphism