Skip to main content

Posts

Showing posts with the label generics

Generics wild card in java

List<Double> or List<Float> cannot be assigned to List<Number> because, List<Double> isn't extend List<Number>. List<Double> dblList = new ArrayList<>(); List<Number> numList = dblList; //illegal To do that we need to use upper-bound wild card. List<Double> dblList = new ArrayList<>(); List<? extends Number> numList = dblList; Same way List<Number> isn't extend List<Object> so, List<Object> cannot be assigned to List<Number> List<Object> objList = new ArrayList<>(); List<Number> numList = objList; //illegal To do that we need to use lower-bound wild card List<Object> objList = new ArrayList<>(); List<? super Number> numList = objList; Upper-bound We can add object of Double type or its sub types to List<Double>. Same way we can add Number or its sub types (e.g. Double, Float, Integer) to List<Number> but, we cannot assign dblLis...