Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generic - ? super... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generic - ? super..." Watch "generic - ? super..." New topic
Author

generic - ? super...

hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54

actually my code says the compiler that 'accept anything that is a super type of class 'c'. so it accepts class 'b'. so far good. but by no means i could add object of class 'a' (which is also a super type) to the list.(since only, the same type of subtype can be added to the list).whereas i have created the list for class 'b' type.
what will the compiler do when i add an object of class 'a' in the adda() method.???
i really could not make it out.. please explain..
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

A workaround to this problem is to cast the Object to be added into c

public void adda(List<? super c> ani)
{
ani.add((c)new Object());//or any of a,b, or c Objects
}


But I am still baffled by this behaviour
George Gates
Ranch Hand

Joined: Jul 11, 2008
Posts: 34
Hi Hari Haran,
I tried the following code trying to add a list of type 'a'. It did not give me any compiler error.


Thanks


Regards,<br />George Gates
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
hi sumit, of course we can case it this way,
ani.add((c)new Object());
but, my question is, my program is purely GENERIC, then we dont need a cast. in this case will the compiler complain??if so, the GENERIC property is not satisfied.. am i right???

hi badal,
what you have told is nice, but that is not my question is..

Thanks for both of you guys.. please let me know if you have any more details..
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

It should work for anything super in the hierarchy


===Vyas Sanzgiri===
My Blog
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
hi vyas, i think the list is actually 'typed' for class b. so how ll it work for class 'a', which is a super class of class b???
List<b> l=new ArrayList<b>();

am i right??
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

It should work for the following if that is your question:-

List<a> l=new ArrayList<a>();
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

actually my code says the compiler that 'accept anything that is a super type of class 'c'. so it accepts class 'b'. so far good. but by no means i could add object of class 'a' (which is also a super type) to the list.(since only, the same type of subtype can be added to the list).whereas i have created the list for class 'b' type.
what will the compiler do when i add an object of class 'a' in the adda() method.???


The interpretation is incorrect. It doesn't "accept anything that is a super type of class 'c'". It accepts something that is a super type of class 'c', but the compiler doesn't know what it is.

Hence, it will only allow you to add something that is of class 'c', or a subtype of class 'c', because those objects are also IS-A super type of all objects of class 'c'.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

Originally posted by Sumit Bisht:
A workaround to this problem is to cast the Object to be added into c

But I am still baffled by this behaviour


While the "work-around" allows you to get around the compiler checks, you also broke it. Why bother using generics, if you are going to work around it?

Henry
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

Well answered by Henry
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
sorry Henry, i really do not understand what do you meant by this,

"Hence, it will only allow you to add something that is of class 'c', or a subtype of class 'c', because those objects are also IS-A super type of all objects of class 'c'."

where does this subtype comes into picture??

Also, do you mean that adding an object of class 'a' is not legal??so it gives a compiler err??or exception???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

where does this subtype comes into picture??


Since the compiler does not know which super type it is to allow, it will only allow you to add objects, which are of *all* the super types. This includes objects which are the subtypes.


Also, do you mean that adding an object of class 'a' is not legal??so it gives a compiler err??or exception???


Why don't you just try it? You will see that adding objects of type 'a' does indeed generate a compiler error. You will also see that adding objects of type 'b' also generate a compiler error -- even though the list is originally defined to take type 'b'.

Henry
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
Thans Henry.. i will surely try my own coding..
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
yes henry, you are perfectly right in your explanation.
but am in desparate need of guidance.
the following is what i learned from kathy book.

public void addAnimal(List<? super Dog> animals)
is essentially, "Hey compiler, please accept any List with a generic type that is of type Dog, or a supertype of Dog.
and,
<? super ...> syntax, you are telling
the compiler that you can accept the type on the right-hand side of super or any of its supertypes.

my case, looks similar to this. but...
can someone explain this please..
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
class a
{
public void adda(List<? super c> l)
{
l.add(new b()); // even i could not add b obj to my list..
}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

// even i could not add b obj to my list..


Which I explained why in a previous post. If you can elaborate why you don't understand the explanation, maybe someone can further help you.

Restating your problem, with an example, doesn't help here... as it has been stated that you shouldn't be able to add a 'b' object to the list. All you did was confirm what was told to you.

Henry
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
Hmmm..after a very long search, the below link gave me the solution for my queries,

http://www.coderanch.com/t/269895/java-programmer-SCJP/certification/super

Anyway hereby i want to THANK HENRY you were very kind replying me. after i went through the above link i found that my issue was really a basic thing in generics.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generic - ? super...