Results 1 to 3 of 3

Thread: Wildtype errors

  1. #1
    gunitinug is offline Member
    Join Date
    Dec 2016
    Posts
    13
    Rep Power
    0

    Default Wildtype errors

    Java Code:
    import java.util.*;
    
    public class A {}
    
    public class B extends A {}
    
    public class C extends B {}
    
    
    public class Test {
    
        
        public void testCoVariance(List<? extends B> myBlist) {
            B b = new B();
            C c = new C();
            myBlist.add(b); // does not compile (1)
            myBlist.add(c); // does not compile (2)
            A a = myBlist.get(0); //(3)
        }
    
        public void testContraVariance(List<? super B> myBlist) {
            B b = new B(); 
            C c = new C();
            myBlist.add(b); //(4)
            myBlist.add(c); //(5)
           A a = myBlist.get(0); // does not compile (6)
        }
    }
    Can you explain this code? I thought (1) and (2) are permissible since <? extends C> is subtype of <? extends B>.
    Also since <? super A> is supertype of <? super B>, only myBlist.add(new A()) and myBlist.add(new B()) is permissible?

    I think this is a whole new operation. I've found this at http://stackoverflow.com/questions/2...onsumer-super:
    Suppose you have a method that takes as its parameter a collection of things, but you want it to be more flexible than just accepting a Collection<Thing>.

    Case 1: You want to go through the collection and do things with each item.
    Then the list is a producer, so you should use a Collection<? extends Thing>.

    The reasoning is that a Collection<? extends Thing> could hold any subtype of Thing, and thus each element will behave as a Thing when you perform your operation. (You actually cannot add anything to a Collection<? extends Thing>, because you cannot know at runtime which specific subtype of Thing the collection holds.)

    Case 2: You want to add things to the collection.
    Then the list is a consumer, so you should use a Collection<? super Thing>.

    The reasoning here is that unlike Collection<? extends Thing>, Collection<? super Thing> can always hold a Thing no matter what the actual parameterized type is. Here you don't care what is already in the list as long as it will allow a Thing to be added; this is what ? super Thing guarantees.
    It's to do with PECS.
    Last edited by gunitinug; 05-12-2017 at 08:59 AM.

  2. #2
    angstrem is offline Member
    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0

    Default Re: Wildtype errors

    You have B and C in a contravariant position.

    The most special function is the one capable to accept an Object.

    The most general function is the one incapable of accepting anything.

    In the first method, add is guaranteed to handle something that extends B. No guarantee that this certain something is B or C.
    .
    In the second method, it is guaranteed to handle a supertype of B. If it can do that, it can also handle B and C, subtypes of this certain something.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Wildtype errors

    I prefer using known classes and relationships so here is my take.

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    
    
    public class Test {
       public static void main(String[] args) {
          List<Integer> ints = new ArrayList<>();
          ints.add(10);
          ints.add(20);
          
          // allowed because List<Integer> is a subtype of List<? extends Number>
          // and any subtype can be assigned to their supertype
          List<? extends Number> nums = ints;
          
          nums.add(10.); // Nope can't add a double to a list of ints.
          // so puts with extends not allowed
          Number d = nums.get(0); // gets with extends are allowed
          
          
          List<Number> n = new ArrayList<>();
          n.add(10);
          n.add(10.2);
          n.add(2.0f);
          
          // allowed because  List<Number> is a subtype of List<? super Integer> 
          // and is a subtype of List<? super Double>
          // and any subtype can be assigned to their supertype
          List<? super Integer> si = n;
          List<? super Double> sd = n;
          
          si.add(10); // puts with super allowed here
          sd.add(10.7); // and here
          
          Double d = sd.get(1); // oops! index 1 of n may not be a Double
          // so gets with super not allowed
       }
    }
    So REPS is what I use (Retreive or get w/Extends and Put w/ Super)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Help with ERRORS
    By Neill in forum New To Java
    Replies: 1
    Last Post: 11-22-2011, 10:48 AM
  2. Can't fix my errors!
    By abdul9383 in forum New To Java
    Replies: 5
    Last Post: 11-21-2011, 08:44 PM
  3. please help, so many errors..
    By mhel_14 in forum New To Java
    Replies: 1
    Last Post: 03-16-2011, 12:26 PM
  4. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  5. Replies: 3
    Last Post: 03-08-2009, 01:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •