Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Questions

 
Wilson Oak
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic