• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics super keyword  RSS feed

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Suppose I have



Why this doesn't work while A is a super type of B ?

 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because List<? super B> doesn't mean "a List that can contain anything that is a supertype of B". It means "a reference to a List of a specific type that is B or a supertype".

So in this case list could be a List<Object>, a List<A> or a List<B>. And you can't add an A object into a List<B>.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swerrgy Smith wrote:Why this doesn't work while A is a super type of B ?

Simply put: you cannot use wildcards to update, for all the reasons Matthew outlined. I suggest you look at the generics tutorial, in particular this section, which describes a possible solution.

Winston
 
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a very well known mnemonics related to super and extends in terms of Generics - PECS (Producer Extends, Consumer Super). The point is, any parameterized type of the form - SomeType<? extends T> is a producer of T, whereas those of the form - SomeType<? super T> is a consumer of T. So, a List<? extends Dog> will give you Dog, whereas a List<? super Dog> will take a Dog.
This doesn't mean that, it can also take any super type of Dog. Because, as already mentioned, that list can actually refer to a List<Animal>, or a List<Dog>, and in both the cases, you can't add an Object to it.
If compiler allowed such addition, then it would certainly fail at runtime. However, you can certainly add a Dog, as any list of super type of Dog, will be capable of holding a Dog.
Let's take a simple example:

Now if the last line would be allowed to compile, then that would certainly throw a ClassCastException at runtime. This is what compiler is trying to avoid.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:There is a very well known mnemonics related to super and extends in terms of Generics - PECS...

Interesting stuff (which I didn't know), but - I suspect - not applicable here.

The fact is that you can't use ANY wildcard when updating a typed Object, because the compiler cannot know whether the object you supplied is actually of the correct type. Reading is something entirely different.

The second link I supplied may provide a solution, but I'm afraid I haven't studied it closely enough to be sure.

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: but - I suspect - not applicable here.

Might not be completely applicable, but certainly relevant, and will be useful for OP. Understand the concept of PECS is very important to understand the usage and restrictions of bounded wildcards.

Winston Gutkowski wrote:The fact is that you can't use ANY wildcard when updating a typed Object, because the compiler cannot know whether the object you supplied is actually of the correct type. Reading is something entirely different.

I guess you're missing my point. It's not that you can't add anything to a List<? super Dog>. You can certainly add a Dog. Compiler would be happy with it. I mentioned about super to relate both the concepts, as super is in fact the other way round. One can weigh both the possibilities, and figure out what exactly is relevant for a particular problem.

Thanks
Rohit
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:I guess you're missing my point. It's not that you can't add anything to a List<? super Dog>. You can certainly add a Dog. Compiler would be happy with it...

OK, but that's not what Swerrgy's example was about. His example took a List<? super B>, and then tried to add something other than a B to it.

I think we're dancing around the same point, but if List<? super Dog> only works if the object in question is a Dog, then the wildcard is bascally redundant; which is why I say: don't use it for updating.

Two minds with the same message; just a different way of saying it.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!