Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

CSG 11 - Chapter 14 - Working With Generics - p.651(Kindle): Muddled and confusing disscusssion ?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or perhaps it is really just a “typo” in the following segment from the book,

**  BEGIN EXCERPT  **
Now let's try an example with an interface. We have an interface and two classes that implement it.

interface Flyer { void fly(); }
class HangGlider implements Flyer { public void fly() {} }
class Goose implements Flyer { public void fly() {} }

We also have two methods that use it. One just lists the interface, and the other uses an upper bound.

private void anyFlyer(List<Flyer> flyer) {}
private void groupOfFlyers(List<? extends Flyer> flyer) {}

Note that we used the keyword extends rather than implements. Upper bounds are like anonymous classes in that they use extends regardless of whether we are working with a class or an interface. You already learned that a variable of type List<Flyer> can be passed to either method.
  ==> A variable of type List<Goose> can be passed only to the one with the upper bound. <== (My Emphasis)
This shows one of the benefits of generics. Random flyers don't fly together. We want our groupOfFlyers() method to be called only with the same type. Geese fly together but don't fly with hang gliders.
**  END EXCERPT  **

As I look at it, the more it seems obvious that the parameter to groupOfFlyers() should be of type, List<? extends Goose>, and the point being made is rather trivial.  Otherwise, I can make neither heads nor tails of it.  Could you please confirm whether it is in fact a mistake, in which case I have been reading too much into it.

Thank you for your attention.  (And thank you for your invaluable book and your commitment of continued support of it!)

Joe Sock (Aspiring Java Developer)

 
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've moved this the OCP forum. (The developer forum was for a retired exam.)

I don't think the code is a mistake. Consider the following



THis matches the text where geese can't be passed. Changing the method signature makes a different point.
 
Jock Soe
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, of course.  (Once again) I was conflating the meaning of a generic type specification (a template or pattern into which a candidate type must fit in order to pass - my  definition) with that of the type of prospective object being assignable to a given type.  So, Goose does not pass the <Flyer> test even though it is assignable or an “instanceof” it.  I think that I have been falling into that confusion becuse the bounded wildcard does, itself, use the test, “instanceof”, as part of the specifictation.

Thank you so much for your attention!  (As Scarecrow says) I should have thought of it myself, as usual.
Joe Sock







 
Jeanne Boyarsky
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a confusing topic! Good to ask for clarification.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic