Win a copy of Kotlin for Android App Development this week in the Kotlin 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Possible OCPJP8 correction, chapter 3 page 120?  RSS feed

 
Greenhorn
Posts: 7
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 39129
723
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 39129
723
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, your explanation is correct
 
Juerg Bauman
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • 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>.
 
permaculture is largely about replacing oil with people. And one tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!