• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Question on Upper-Bounded Wildcards

 
Ranch Hand
Posts: 149
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So from the OCP Java17 they give an example on what method call will compile, "line a" or "line b"?



I wanted to check with you guys if my way of "reasoning" is correct:

First I notice I'm dealing with two classes which have common interface, also I notice I'm dealing with parameterized collections. So I envision three groups of hierarchies like so:



First lets check anyFlyer.

Since formal parameter of anyFlyer is List<Flyer> the object it points to, if we were to write it somewhere in the code, it could only:



It couldn't be:



This is because of parameterized types (I'm not sure are there any more reasons or is this correct):



So flyer can only be pointing to appropriate parameterized collection.

Having that  in mind, passing variable x, to anyFlyer would cause compilation error.

Lets check groupOfFlyers:

If we take a look at third group of hierarchies, we can see 'List<? extends Flyer>'. These can be type for (I don't know proper term) four objects (options ?), that would compile:



And since they all compile and x is one of them, f1, then we can safely say "Line b" is the one which does compile.


I'm interested to hear is this good way to reason about?



 
Saloon Keeper
Posts: 15714
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't really understand your entire reasoning, but I think you have figured out something for yourself that works. The answer you got is correct, anyway.
 
Sheriff
Posts: 8890
638
Mac OS X VI Editor BSD Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP

I did not try to disassemble your example, but here is some guide for you using Java's built-in types. Try to relate it to your case and see if you can make sense out of it.

Using extends (i.e. List<? extends T>)

For example, the wildcard declaration of List<? extends Number> foo means that any of these are legal assignments:

Reading (get) - given the above possible assignments, what type of objects you are guaranteed to read from a List foo?
can
  • You can read a Number because any of the lists that could be assigned to foo contain a Number or a subclass of Number
  • can't
  • You can't read an Integer because foo could be pointing to an ArrayList<Double>
  • You can't read a Double because foo could be pointing to an ArrayList<Integer>

  • Writing (add) - what type of objects you could add to a List foo that would be legal for all the above possible List instances?
    can't
  • You can't add an Integer because foo could be pointing to an ArrayList<Double>
  • You can't add a Double because foo could be pointing to an ArrayList<Integer>
  • You can't add a Number because foo could be pointing to an ArrayList<Integer>

  • In different words, you can't add any object to List<? extends T> because you can't guarantee what kind of List it is actually pointing to, so you can't guarantee that the object is allowed in that List. The only guarantee so to speak is, that you can only read from it and you'll get a T or subclass of T. That is it.


    Now, using super (i.e. List<? super T>)

    For example, the wildcard declaration of List<? super Integer> foo means that any of these are legal assignments:

    Reading (get) - given the above possible assignments, what type of object are you guaranteed to receive when you read from a List foo:
    can
  • You can read an Object because all classes inherit from Object
  • can't
  • You can't guarantee an Integer because foo could be pointing to an ArrayList<Number> or ArrayList<Object>
  • You can't guarantee a Number because foo could be pointing to an ArrayList<Object>

  • The only guarantee is that you will get an instance of an Object or subclass of Object, but you don't know what that subclass might be.

    Writing (add) - what type of object could you add to a List foo that would be legal for all the above possible List instances:
    can
  • You can add an Integer because an Integer is allowed in any of the above lists
  • You can add an instance of a subclass of Integer because Integer is allowed in any of the above lists
  • can't
  • You can't add a Double because foo could be pointing to an  ArrayList<Integer>
  • You can't add a Number because foo could be pointing to an ArrayList<Integer>
  • You can't add an Object because foo could be pointing to an ArrayList<Integer>


  • All can get confusing, but when you think about it slowly, all make sense.
     
    Mike Gosling
    Ranch Hand
    Posts: 149
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey Liutauras, thanks for excellent post. After reading Oracle's tutorial on generics it became a bit clearer, but now with your post, these wildcards are becoming far more clearer now. I will just follow your strategy for determining what can be added/removed in generics with bounded wildcards.

    Good thing you dismantled my example  

     
    Ranch Foreman
    Posts: 662
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:@OP
    List<? super Integer> foo = new ArrayList<Integer>(); // Integer is a "superclass" of Integer (in this context)

  • You can add an Integer because an Integer is allowed in any of the above lists



  • I have been struggling to understand this concept. Isn't the code snippet below, similar to what you wrote above?
    Yet it does not compile

    If you think it is useful, please vote to reopen the question here https://stackoverflow.com/questions/73889515/does-wildcard-super-number-refer-to-a-superclass-like-object-or-a-subtype-li

     
    Marshal
    Posts: 28296
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anil Philip wrote:I have been struggling to understand this concept. Isn't the code snippet below, similar to what you wrote above?
    Yet it does not compile


    You can add an Integer to a List<? super Integer>, as he wrote above. Likewise you can add a B to a List<? super B>. But your code doesn't do that.
     
    Liutauras Vilda
    Sheriff
    Posts: 8890
    638
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anil Philip wrote:Isn't the code snippet below, similar to what you wrote above?


    It is, but not the same. Anyway, reasons are the same. If I did not make any mistake, that should be it. Please write code and verify for yourself.

    You have List<? super B>

    Reading:
    can
  • You can read an Object because all classes inherit from Object

  • can't
  • You can't guarantee a B because foo could be pointing to an ArrayList<A> or an ArrayList<Object>
  • You can't guarantee a A because foo could be pointing to an ArrayList<B> or an ArrayList<Object>


  • Writing:
    can
  • You can add a B
  • You can add an instance of a subclass of B

  • can't
  • You can't add an A because foo could be pointing to an  ArrayList<Object>

  •  
    Anil Philip
    Ranch Foreman
    Posts: 662
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:You can't add an A because foo could be pointing to an  ArrayList<Object>



    But the compiler knows that we are only adding an A to a List<A> yet it disallowed it!
    Does List<? super B> mean every member should be the same (all As, all Bs, all Objects)
    Is my understanding correct?
     
    Mike Gosling
    Ranch Hand
    Posts: 149
    1
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hopefully this would help.



    Please go through this tutorial (https://docs.oracle.com/javase/tutorial/java/generics/index.html) and combined with this thread it should make sense. It helped me I am know seeing that List<? super T> is more convenient for addition and that List<? extends T> is more handy for getting things.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15714
    367
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anil Philip wrote:But the compiler knows that we are only adding an A to a List<A> yet it disallowed it!


    The compiler knows no such thing. It only knows that you have a List<? super B>, which could be a List<A>, but it also could be a List<B>. The compiler won't know which one, so it doesn't allow you to add an A to what might possibly be a List<B>

    Does List<? super B> mean every member should be the same (all As, all Bs, all Objects)
    Is my understanding correct?


    No. List<? super B> means it can refer to any kind of list, as long as the generic type argument is B or a supertype of B. So, it may refer to a List<B>, a List<A> or a List<Object>, but importantly the compiler will not know which one exactly.

    It's not helping that you've chosen type names that are meaningless. Let's use different examples:


    Here is a simple case that should be easy to understand:

    Let's mix some big cats:

    So far so good, right? Now let's try some lower bounds:
     
    Anil Philip
    Ranch Foreman
    Posts: 662
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:

    It's not helping that you've chosen type names that are meaningless. Let's use different examples:


    I tried to compile your code to see what is allowed, but there seem to be other compile errors.


     
    Liutauras Vilda
    Sheriff
    Posts: 8890
    638
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Remove final from line 17.

    The remaining non-compiling parts of the code are explained in the comment that they are not compiling. That was intention of this code, to demonstrate the problem domain.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15714
    367
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry, Liutauras is correct. I habitually added the final modifier to that code, and didn't test it before posting.
     
    Anil Philip
    Ranch Foreman
    Posts: 662
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:@OP All can get confusing, but when you think about it slowly, all make sense.



    How about this?


    However, this will compile and run and is effectively an



    In other words, it has defeated the original restriction.
     
    Liutauras Vilda
    Sheriff
    Posts: 8890
    638
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anil Philip wrote:

    In other words, it has defeated the original restriction.


    I don't get your point, nor what's confusing you. But it didn't defeat what I mentioned before, if that's what you meant.

    Apart from the fact that you declared oList but adding to cList that code wouldn't compile, but if you were to fix it, it would compile just fine, because C is a subclass of B. We have explained that thrice.
     
    Anil Philip
    Ranch Foreman
    Posts: 662
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:We have explained that thrice.


    no need to get annoyed


    is illegal, and I get it.

    However, one is still able to construct effectively the same thing by doing

     
    Liutauras Vilda
    Sheriff
    Posts: 8890
    638
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ah no no, I'm not annoyed, apologies if I sounded that way

    Let's get back to business

    You know, when you say new ArrayList<C>(), you say that this array list would hold C's. But that's not true, right? Because on the left side you say List<? super B>.

    However, when you have an empty diamond operator <>, it infers the type this array list could hold based on the declaration on the left hand side, then it compiles just fine, and I trust you understand why to add C is allowed.
     
    Marshal
    Posts: 79707
    381
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    But just try getting a B or a C back out of that List
    How can you get this code to compile?
     
    Anil Philip
    Ranch Foreman
    Posts: 662
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:Ah no no, I'm not annoyed, apologies if I sounded that way

    Let's get back to business

    You know, when you say new ArrayList<C>(), you say that this array list would hold C's. But that's not true, right? Because on the left side you say List<? super B>.

    However, when you have an empty diamond operator <>, it infers the type this array list could hold based on the declaration on the left hand side, then it compiles just fine, and I trust you understand why to add C is allowed.



    My point is that in the first case, an ArrayList of C was not allowed because of the declaration on the left hand side.
    But if we still keep the declaration the same and instead add the C's one by one, or bulk copy, we end up with the result that was denied earlier - an ArrayList of Cs.
     
    Liutauras Vilda
    Sheriff
    Posts: 8890
    638
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anil Philip wrote:...add the C's one by one, or bulk copy, we end up with the result that was denied earlier - an ArrayList of Cs.


    I know I know what you mean. But that's not accurate.

    Consider this:

    Now if you say new ArrayList<C>(), that contradicts what List<? super B> says:
    "You can add a B or an instance of a subclass of B"

    F is indeed a subclass of B, same as C is a subclass of B.

    Is it clear now?
     
    Anil Philip
    Ranch Foreman
    Posts: 662
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:

    Anil Philip wrote:...add the C's one by one, or bulk copy, we end up with the result that was denied earlier - an ArrayList of Cs.


    I know I know what you mean. But that's not accurate.

    Consider this:

    Now if you say new ArrayList<C>(), that contradicts what List<? super B> says:
    "You can add a B or an instance of a subclass of B"

    F is indeed a subclass of B, same as C is a subclass of B.

    Is it clear now?



    If I understand what you are saying,
    we should be able to have both F's and C's in the ArrayList since both are subclasses of B.
    The only way to have this is to deny or disallow .
    Am I understanding you correctly?
     
    Saloon Keeper
    Posts: 5533
    213
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Congratulations, Mike!

    Your topic got a mentioning in the October 2022 Journal, and so you earn a cow.

    (Edit: that'll be tomorrow, since I have reached my cow-limit)
     
    Anil Philip
    Ranch Foreman
    Posts: 662
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anil Philip wrote:
    Am I understanding you correctly?


    @Liutauras Vilda can you please confirm?
     
    Liutauras Vilda
    Sheriff
    Posts: 8890
    638
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anil Philip wrote:If I understand what you are saying,
    we should be able to have both F's and C's in the ArrayList since both are subclasses of B.
    The only way to have this is to deny or disallow

    new ArrayList<C>()

    Am I understanding you correctly?


    Well, yes, otherwise contract isn't respected.

    Apologies didn't reply earlier - missed your message.
     
    Anil Philip
    Ranch Foreman
    Posts: 662
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:

    Anil Philip wrote:If I understand what you are saying,
    we should be able to have both F's and C's in the ArrayList since both are subclasses of B.
    The only way to have this is to deny or disallow

    new ArrayList<C>()

    Am I understanding you correctly?


    Well, yes, otherwise contract isn't respected.

    Apologies didn't reply earlier - missed your message.


    Thank you for your patience and kindness.
     
    Create symphonies in seed and soil. For this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic