Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Problem

 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edited question from K&B Generics study material:



When <? super MyDog> allows any generic type higher in inheritance hierarchy to be passed to the method addAnimal(), then why doesn't it add any object higher in hierarchy than MyDog?
[ September 04, 2008: Message edited by: Somnath Paul ]
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somnath what are you doing. I was banking on you that you will solve my generics related problems.

Anyway I know this one.... I also know generics

Well enough of laughing....

If it would allow elements from higher than MyDog to be added to it then it will create type safety problems.

suppose you are calling the method with List<Dog> and then you add an Animal(legal super type of MyDog) to the list in that method. then when you assign that value to a Dog reference it will throw a ClassCastException.


public void addAnimal(List<? super MyDog> animals)
{
animals.add(new Animal());//breaks type safety
}
main()
{
List<Dog> dList = new List<Dog>();
addAnimal(dList);
Dog d = dList.get(0); //ClassCastException

This is why when you use ? super XYZ, you are only allowed to add elements of type XYZ or it's sub-types...

If you understand it then I am the king of the world
[ September 04, 2008: Message edited by: Ankit Garg ]
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when you use ? super XYZ, you are only allowed to add elements of type XYZ...


you can add subtypes also
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realized my mistake so updated my post before you corrected me....

But anyway it looks like what I said was correct....I have climbed the first step of Generics .
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit, I am very new to generics like you(probably) and learning it for the first time. And its very confusing. In the next week you would find me posting doubts and they may be easy.

Suppose this is the main method along with the classes given in the original post.



My question is: when we can add an Dog object and pass it to the addAnimal method, what is the use of <? super MyDog> ?? It would not give any error.
There would anyways be ClassCast exception since the AddAnimal() takes MyDog and above and the List actually contains an object of Dog which is higher in hierarchy than MyDog.
The program dosent even give any warnings!
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't get it. The program doesn't allow you to add anything above MyDog into the list if the list is of type List<? super MyDog>. This is just to avoid the ClassCastException.

The use of this technique is that it will allows you to send List which is a generic version of any super type of any type that you provide.
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotcha..


 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also remember that you will not have any ClassCastException in the addAnimal method as if you try to take any element out of the list, then you will have to assign it to a reference of type Object.

public void addAnimal(List<? super MyDog> animals)
{
Object obj = animals.get(0); //OK
MyDog mDog = animals.get(0); //Error as animals can contain elements of type higher than MyDog in Hierarchy.
}
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! I was just few minutes late, I explained about something only to find that you have already posted a code about the same thing...
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, very true, the engineer's designed it in such a manner.. they knew that if its an Object reference, nothing can be higher than that, so that is okay to assign to, whereas any other reference you try to assign the reference may contain an object which may be higher in inheritance hierarchy and may NOT be type-safe. So assigning any other reference other than Object causes the compiler to complain.
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Somnath we have climbed the first step for Generics. Time for Party
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic