• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic - ? super...

 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should work for anything super in the hierarchy
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should work for the following if that is your question:-

List<a> l=new ArrayList<a>();
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well answered by Henry
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thans Henry.. i will surely try my own coding..
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic