Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Generics

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the book I am reading there is written:


Wildcards can be bounded. For example, <? extends Runnable> specifies that ? can match
any type as long as it is Runnable or any of its derived types. Note that extends is inclusive, so
you can replace X in ? extends X. However, in <? super Runnable> , ? would match only the
super types of Runnable, and Runnable itself will not match (i.e., it is an exclusive clause).



I just want to confirm if the bold part is is wrong or else I am understanding it wrongly. Given the below:



Line 2 compiles successfully. Therefore is the statement "and Runnable itself will not match" correct within the given context?
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No - the book is incorrect. Which book is it? That's not a good sign!
 
Omni Muss
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:No - the book is incorrect. Which book is it? That's not a good sign!



Thanks you for your answer. The book is Oracle Certified Professional Java SE 7 Programmer Exam.
To be honest this is the one error I found so far.
 
Omni Muss
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just want to confirm I understand <? super B> correctly.

Am I right to assume that List<? super B> ONLY make sense to use when you want to add elements to that List which are of type B or a class that extends B? I am making this assumption as a List<? super B> cannot return anything other then Object; i.e. - the following would fail:



I understand why the line marked as "Not OK" doesn't work, as a List<? super B> can be a List<A> and therefore "B b = l.get(0)" would fail. I just want to confirm my understanding / assumption of where it is right to use <? super X> is right or wrong.

?
 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Omni Muss wrote:I just want to confirm I understand <? super B> correctly.

Am I right to assume that List<? super B> ONLY make sense to use when you want to add elements to that List which are of type B or a class that extends B?



No, that's wrong. List<? super B> means you have a List whose elements are of some class X which is a superclass of B. Not a class which extends B, that would be a subclass of B. And remember that B is classified as a superclass of B when you're talking about generic wildcards.

Edit:

As for adding elements to such a list, remember that the compiler doesn't know anything about the class X except that it's a superclass of B. So you can add an object to the list provided that it's a X, i.e. it's any object which is a subclass of X. In the example we know that B is a subclass of X, so we can add a B element to the list. Likewise if C is a subclass of B then it's a subclass of X, so we can add a C element to the list.
 
Omni Muss
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But from a practical point of view the only thing you can do with a List<? super B> is add objects of type B or extend from B right? What other uses are there for <? super B>?

Paul Clapham wrote:

Omni Muss wrote:I just want to confirm I understand <? super B> correctly.

Am I right to assume that List<? super B> ONLY make sense to use when you want to add elements to that List which are of type B or a class that extends B?



No, that's wrong. List<? super B> means you have a List whose elements are of some class X which is a superclass of B. Not a class which extends B, that would be a subclass of B. And remember that B is classified as a superclass of B when you're talking about generic wildcards.

Edit:

As for adding elements to such a list, remember that the compiler doesn't know anything about the class X except that it's a superclass of B. So you can add an object to the list provided that it's a X, i.e. it's any object which is a subclass of X. In the example we know that B is a subclass of X, so we can add a B element to the list. Likewise if C is a subclass of B then it's a subclass of X, so we can add a C element to the list.

 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic