*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Problem" Watch "Generics Problem" New topic
Author

Generics Problem

Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
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 ]

Preparing for SCJP 6.0
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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 ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210


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


you can add subtypes also


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: May 19, 2008
Posts: 177
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: May 19, 2008
Posts: 177
Gotcha..


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: May 19, 2008
Posts: 177
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

So Somnath we have climbed the first step for Generics. Time for Party
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Problem
 
Similar Threads
Why is not working
Generics Question
Generics - super
Generics question from bonus exam
Generics - return types