Incompatible types List of List and ArrayList of ArrayList

Kraken picture Kraken · Jul 17, 2014 · Viewed 38.8k times · Source

The below line gives me error :

Incompatible Types.

List<List<Integer>> output = new ArrayList<ArrayList<Integer>>();

What is the reason?

EDIT

I understand if I change my second ArrayList to List, it does not give me error. I want to know the reason of error though. Thanks

Answer

earthmover picture earthmover · Jul 17, 2014

From Generics, Inheritance, and Subtypes

This is a common misunderstanding when it comes to programming with generics, but it is an important concept to learn.

enter image description here

Box<Integer> is not a subtype of Box even though Integer is a subtype of Number.