• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Possible OCPJP8 correction, chapter 3 page 120?

 
Greenhorn
Posts: 7
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the explanation below the middle code block on page 120, the book states:

"Line 7 doesn't compile because we can't add a Sparrow to List<Bird>"

I'm not sure if that's correct or effectively explaining that case, since based on the code above, a Sparrow could technically be added to a List<Bird> like:

 
Ranch Hand
Posts: 44
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,

Technically, what you say is true. Since the List is a list of Bird, we could add instances of Sparrow to it (because of inheritance). So, while the surrounding explanation is correct, the sentence you mention has to be understood this way:

Line 7 doesn't compile because at compile time Java doesn't know what the real type of the list is (it could be Bird, Sparrow, or even another subclass of Bird or Sparrow)


And you agree that if Parrot is another subclass of Bird (next to Sparrow), then I can't possibly add an instance of Sparrow to it.

But you're right, while Java doesn't allow that, it should be clearer as to why it doesn't.

Guillaume
 
author & internet detective
Posts: 40747
827
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,
You are correct that is an errata. It should say line 7 is incorrect because you can't add a Sparrow to a a List<? extends Bird>. Good catch!
 
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really understand the explanation of the first compiler error.

birds can be: (1)List<Bird> or (2)List<Sparrow> or (3)List<"another possible subclass of Bird">
birds.add(new Sparrow()):  List<Bird> and List<Sparrow> are ok, so the line doesn't compile because birds could be of List<"another possible subclass of Bird">
birds.add(new Bird()): does not compile because birds could be of List<Sparrow> or List<"another possible subclass of Bird">

Line 7 doesn’t compile because we can’t add a Sparrow to List<? extends Bird>...


Is this explanation correct?
 
Jeanne Boyarsky
author & internet detective
Posts: 40747
827
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
Yes, your explanation is correct
 
Juerg Bauman
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. For me then this explanation isn't helpful at all. It describes the result - a compiler error, but doesn't explain anything.

Line 7 doesn’t compile because we can’t add a Sparrow to List<? extends Bird>...


I suggest something like:
Line 7 doesn't compile because we can't add a Sparrow to a List of some other subclass of Bird, like f.e. List<Parrot>.
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic