Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wildcard Type with Extend - Generics

 
James Johnson Jr
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following three classes:



To learn about wildcards, I tested this code in a class as follows:



I get a compile error as follows:



I would be very grateful for any help in understanding why this is an error.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable "u" is of the class "Util<X>" where X is some unknown subclass of Employee. Its set() method expects a parameter of type X. It may not be possible to assign an Employee object to something of type X, so that's why it's illegal.
 
James Johnson Jr
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham, thanks for the quick reply.

Unfortunately, I don't quite understand why that would be an issue. My understanding is that Employee is a subclass of itself. So, if X is some subclass of Employee, then I'm not sure why it would be illegal to assign to X. For what it's worth, this also fails with the same type of error if I use Manager as follows:



Sorry if I'm misunderstanding your reply.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying that you believe that

is valid code whenever X is a subclass of Employee? And also that

is valid whenever X is a subclass of Employee?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Johnson Jr wrote:Unfortunately, I don't quite understand why that would be an issue. My understanding is that Employee is a subclass of itself. So, if X is some subclass of Employee, then I'm not sure why it would be illegal to assign to X.


But X could be any subclass of Employee.

Look at it another way. Would you expect the following to compile?

That's essentially what's happening - or at least, it could be, if X is CodeMonkey. The compiler will only allow things it can guarantee are type safe, and that means type safe for any valid value of X.
 
James Johnson Jr
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for the replies.

I was viewing the set method as effectively becoming:



in the case of u. Then, I mistakenly thought that any parameter that extends Employee (such as Manager) should be able to pass. In other words the method would effectively be:



Instead, ? extends Employee represents a subclass of Employee. It is unknown which subclass so the call to set fails.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. That's an extremely common misconception about wildcards in generics, so you aren't the first one to fall into that trap.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my humble opinion, whenever you use a generic declaration as extends SomeThing that SomeThing should be of a type that is abstract and not possible to instantiate in itself.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea of the wildcard is not so much that the element is a subtype of something, but that the collection is a subtype of another.
So if we have public class Cherry extends Fruit, and List<Cherry> then List<Cherry> is not a subtype of List<Fruit>. But List<? extends Fruit> is a subtype of List<Fruit>.
It's in the Java Tutorials; look for "subtyping" and "wildcards".
 
James Johnson Jr
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the additional comments, but I have one concern in regards to:

Campbell Ritchie wrote:But List<? extends Fruit> is a subtype of List<Fruit>.


Shouldn't it be List<Fruit> is a subtype of List<? extends Fruit>?
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to go through the tutorials link I gave you earlier. Try this section too. The subtype and wildcard pages might explain it better. Sorry, I got confused because there are two generics sections in the tutorials.

I think you are probably correct. Have a look in the tutorials.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic