• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic Questions

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.devarticles.com/c/a/Java/Wildcards-and-Generic-Methods-in-Java/1/
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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......
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic