This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Upper-bounded wildcards specific case

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page 172 from OCP guide, chapter 3 Generics and Collections, states:



The problem stems from the fact that Java doesn’t know what type List<? extends
Bird> really is. It could be List<Bird> or List<Sparrow> or some other generic type that
hasn’t even been written yet. Line 7 doesn’t compile because we can’t add a Sparrow to
List<Bird>, and line 8 doesn’t compile because we can’t add a Bird to List<Sparrow>.
From Java’s point of view, both scenarios are equally possible so neither is allowed.



I didn't get the point, why that 2 lines does not compiles, cause from my point of view,
Sparrow extends Bird, and a list of Type Bird must accept a Bird.

Any help will be welcome.
 
Rancher
Posts: 3371
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thiago Medeiros wrote:... a list of Type Bird must accept a Bird.

Any help will be welcome.


The problem is, the compiler doesn't know that it has a List of Birds.  The declaration of the variable "birds" says it's a List<? extends Bird>.  As far as the compiler is concerned, it might be a List<Bird> or a List<Sparrow> or a List<RedFoxSparrow> or something else - it has no idea.  When later code tries to add a Sparrow or Bird, the compiler can't guarantee that the List will accept that, because the List might be a type that can't accept Sparrow or Bird.

Generally, if you have a List<? extends Foo> you can take things out of it, and the compiler will know that those things are some type of Foo.  But you can't put things in to the list, because the compiler can't know what the ? in List<? extends Foo> actually represents.  It could be something more specialized than the type you're trying to add. <br />
 
Bartender
Posts: 3517
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you have a class 'OneWingedSparrow extends Sparrow'. Then it is possible that you have a List<OneWingedSparrow>, and it makes sense that it is NOT allowed to add a normal Sparrow to that list. As far as the compiler concerns, List<? extends Bird> might be that OneWingedSparrow List.

For the same reason you cannot add a Bird to that list.

What about the other way? Can you add a OnrWingedSparrow to a List<Bird>?
 
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the compiler doesn't allow the following:

Then I am wondering, in what type of scenario would one use List<? extends X>?

Also, is this what is meant by "the List becomes logically immutable"?
 
Enthuware Software Support
Posts: 4351
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brecht Geeraerts wrote:If the compiler doesn't allow the following:

Then I am wondering, in what type of scenario would one use List<? extends X>?

Also, is this what is meant by "the List becomes logically immutable"?


This short writeup answers your question:
https://enthuware.com/resources/oracle-java-programmer-certification-resources/173-key-concepts-in-understanding-generics
 
Police line, do not cross. Well, this tiny ad can go through:
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!