Suppose we have class A and class B which extends A. Then consider:

 

B b = new B();

// this line works just fine
A a = b;
List<B> listOfB = new ArrayList<>();

// this line results in
// compile-time error
List<A> listOfA = listOfB;

 

The point being that although b is a subtype of a,

List<B> is not an subtype of List<A>. Instead they are both subtypes of List<?>.

 

List<? extends Integer> int:ist = new ARrayList<>();

// This code will pass
List<?> wildList = intList;

List<? extends Object> objList = intList;
List<? extends Number> numList = intList;

 

<?> is the same as <? extends Object> as Object is the top class in Java

'Java - English ver' 카테고리의 다른 글

Writing generic methods  (0) 2024.11.06
Writing genenric methods  (0) 2024.11.05
Using lower bounded wildcards  (0) 2024.11.03
Upper bounded wildcards  (0) 2024.10.31
Using unbounded wildcards  (1) 2024.10.30

+ Recent posts