Answer the question
In order to leave comments, you need to log in
What is the point of masks in Java generics?
For example, in the following example, there will be a compile-time error on all lines where add() is used:
public class Main {
public static void main(String[] args) {
List<? extends Fruit> fList = new ArrayList<>();
List<?> nList = new ArrayList<>();
fList.add(new Apple()); // Apple extends Fruit
fList.add(new Fruit()); // Even Fruit itself cannot be added
fList.add(new Object());
nList.add(new Object());
nList.add(new Integer(5));
}
}
List<? extends Fruit> fList = new ArrayList<>();
// Здесь коллекция позволяет добавить любой класс ниже Fruit по иерархии ( включительно )
List<? super Fruit> fList = new ArrayList<>();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question