File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Wildcard Type with Extend - Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wildcard Type with Extend - Generics" Watch "Wildcard Type with Extend - Generics" New topic
Author

Wildcard Type with Extend - Generics

James Johnson Jr
Greenhorn

Joined: Sep 27, 2011
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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

Joined: Sep 27, 2011
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Sep 27, 2011
Posts: 4

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
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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

Joined: Mar 10, 2008
Posts: 326

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Sep 27, 2011
Posts: 4
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wildcard Type with Extend - Generics