Jock Soe

Greenhorn
+ Follow
since Aug 20, 2020
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jock Soe

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







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)

With an “operator followed by assignment” operator, the compiler throws in an implicit cast of the RHS value to the LHS type.  (You can sort of reason why why it should or why it should not be that way, but the bottom line is, as usual, “thems are the specs.”)

Joe Sock - i hope that helps!
2 years ago