The next wildcard ArrayList <? Can I add an instance of the Fruit parent class to the super Fruit > list?

public class Food {
}

public class Fruit extends Food {
}

public class Apple extends Fruit {
}

ArrayList<? super Fruit> arrayList = new ArrayList<>();

arrayList.add(new Apple()); //
arrayList.add(new Fruit()); //
        
arrayList.add(new Food()); //

Why did I report an error?

the reason I have this problem is this:

static void set(Pair<? super Integer> p,Integer first,Integer last){
        p.setFirst(first);
        p.setLast(last);
}

public static void main(String[] args) {
        PairHelp.set(new Pair<Integer>(1,2),1,2);
        PairHelp.set(new Pair<Number>(1,2),1,2); //
}

what"s the difference between the two?

Mar.24,2021

? Super Fruit: can be the parent class of Fruit or Fruit, but the parent class cannot determine what type it is, but it can be determined that the subclass of Fruit must be of type Fruit, so only add Fruit and its subclass


cannot be the parent class. To take an extreme example, all classes inherit from Object,. If the parent class can, then all classes can?


No, give ArrayList assigns values, we are not sure which things will be assigned to it, but what is certain is that the minimum element assigned to it is Fruit, so there must be nothing wrong with adding Fruit and its subclasses to it.
by the way, ArrayList means that the highest is Fruit, but the lowest is unlimited, so you can't add elements to it, you can only assign values to the whole

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e599f3-45541.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e599f3-45541.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?