I was wondering why is it not possible to call List<Number>
not with List<Integer
> even Integer is an extended class of the abstract class Number ? There is a logical error as I could call a Method with the parameter Number also with Integer.
public class Que
{
public void enterNumbers(List<Number> nummern)
{
for (Number number : nummern)
{
System.out.println(number + "\n");
}
}
public void enterNum(Number num)
{
System.out.println("This is a number " + num);
}
public static void main(String[] args)
{
Que que = new Que();
Integer myInteger = new Integer(7);
// possible (of course!)
que.enterNum(myInteger);
List<Integer> num = new ArrayList<Integer>();
num.add(4);
num.add(45);
Integer inte = new Integer(333);
num.add(inte);
// not possible !
que.enterNumbers(num);
}
}
To solve it I could work with List<?>
or List<? extends Number>
... so I don't need the solution I want to know the exact reason.
The only solution I could think of List is bind with Number as a new Type of Data Structure.