posted 12 years ago
This wont' compile because p can be Animal, Animal's super type or Object.
What happens if a programmer does this inside addAgainWithSuper method :
List<Animal> l = new ArrayList<Animal>();
l.add(p);
?
What happens if p is an object? The list l cannot add p to it.
Or what happens if a program writes this:
Animal a = p; //where p is an object.
It will have type mismatch compilation error.
Since the programmer can write code in the method that may violation compilation rules or not type safe, the compiler won't let the addAgainWithSuper to be compiled as p may be Animal, Animal's super type or even object. During compilation time, the compiler does not know what type of p the programmer will pass as the argument.
Correct me if I am wrong.