If I have the following method:
public <U extends Number> void doSomething(List<U> l){
}
Then due to type erasure the compiler will make it to doSomething(List<Number> l). Right?
If this is the case, then why it is not possible to declare the following along with this:
public void doSomething(List<?> l){
}
Isn’t this second method, type erased to doSomething(List<Object> l)? Why do I get compiler error of same erasure for these 2 methods?
Your thinking is wrong. Erasure leads to both methods having this signature (
Listargument types being erased):Hence, the collision. What you thought was possible to do is this:
In this case, after erasure, the method signatures will become this (after
Uhaving been erased)In this case, there is no signature collision.