Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Generic Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generic Questions" Watch "Generic Questions" New topic
Author

Generic Questions

Wilson Oak
Greenhorn

Joined: Apr 21, 2005
Posts: 19
Hi all, I get confused with wildcards in generic. Let's see the code



Well, I know why the two add statements of list1 are fails, because Object and Person may not be Driver so that they cannot be added into list1. However, I have no idea why 1-4 are failing. Can some one explain that?

Thanks!


~WOak
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Regarding your first question-
When you say this-

it means it can accept any Object which is driver or super class of driver and you can add those objects in list.

Regarding your second question-
When you say this-

it means list2 can accept any Object which is Driver or subtype of Driver, but you can not add ANYTHING in list.


I am not sure about the explanation of first answer. I will watch this thread for better explanation.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051




while using <? extends driver> list2......using extends means that you cannot add anything to a list....
because you donot know what is driver whether it is a interface or a class.......

like if class A implements Serialization...........
then you have to write like this
....


but super is specifically used for superclass......

like if class A extends B.....

then using super ......List<? super B> list2.....
this can also be done using extends but then you cannot add anything to a list...


thats why


are giving compilation error.....

because compiler doesnot know whether Driver is an interface or class


SCJP6.0,My blog Ranchers from Delhi
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

http://www.devarticles.com/c/a/Java/Wildcards-and-Generic-Methods-in-Java/1/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

shanky sohar wrote:while using <? extends driver> list2......using extends means that you cannot add anything to a list....

With one exception: null

because you donot know what is driver whether it is a interface or a class.......

because compiler doesnot know whether Driver is an interface or class

Sure it does. It knows that Driver is a class that extends Person. However, with "? extends Driver" the type could be Driver, StudentDriver, RaceCarDriver, or anything else that extends Driver. You don't know which one and that's why you can't add anything.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rob Prime wrote:
shanky sohar wrote:while using <? extends driver> list2......using extends means that you cannot add anything to a list....

With one exception: null

that is Rob
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Rob Prime wrote:
shanky sohar wrote:while using <? extends driver> list2......using extends means that you cannot add anything to a list....

With one exception: null

because you donot know what is driver whether it is a interface or a class.......

because compiler doesnot know whether Driver is an interface or class

Sure it does. It knows that Driver is a class that extends Person. However, with "? extends Driver" the type could be Driver, StudentDriver, RaceCarDriver, or anything else that extends Driver. You don't know which one and that's why you can't add anything.


Thanks for correction...see i have edited my post with your correction......
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

but it is written in kathy book that
[i][b]First, the <? extends Animal> means that you can take any subtype of Animal;
however, that subtype can be EITHER a subclass of a class (abstract or concrete)
OR a type that implements the interface after the word extends. In other words,
the keyword extends in the context of a wildcard represents BOTH subclasses and
interface implementations. There is no <? implements Serializable> syntax. If
you want to declare a method that takes anything that is of a type that implements
Serializable, you'd still use extends like this:
// odd, but correct
// to use "extends"
This looks strange since you would never say this in a class declaration because
Serializable is an interface, not a class. But that's the syntax, so burn it in!

One more time—there is only ONE wildcard keyword that represents both
interface implementations and subclasses[/b][/i]

then how can compiler check that is a class...as Extends represent both class and interface
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

while using <? extends driver> list2


so i think i was right.......the compiler doesnot even know whether driver is class or interface.....
so how could it knows that the subtype which is going to add is a class or interface......
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

shanky sohar wrote:Thanks for correction...see i have edited my post with your correction......

Please don't do that; it makes things *very* confusing, particularly when you simply cut-and-paste somebody else's text and present it as your own.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

David Newton wrote:
shanky sohar wrote:Thanks for correction...see i have edited my post with your correction......

Please don't do that; it makes things *very* confusing, particularly when you simply cut-and-paste somebody else's text and present it as your own.


okay....now i onwards i will be careful..............i have made my post looks like the same which i have posted earlier
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Questions