• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics + Polymorphism

 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Jonides
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understand is that you can only put subtype of E to the list if the list is declared as List<? super E>.
 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic