The declaration
GenericP<? extends Number> gp
says that anything can go in this gp which passes the "is a"
test for Number. You created the class GenericP to hold only Integer (new GenericP<Integer>
, if the method call gp.aMethod would have succeeded then anything which is a Number could be passed to that method which is supposed to accept only Integer and all your type safety will be lost, to avoid such situation the above code is illegal. Following is just a variation of the declaration
GenericP<? super Number> gp = new GenericP<Number>();
gp.aMethod(new Integer(5));
gp.aMethod(new Double(5));
in which case your type safety is assured and the compiler is also happy.
Hope I am clear.