I am trying to learn Java Generics. I wrote the following code and was trying to understand how compiler apply Type erasure rules on different scenarios of codes.
Here is the code:
package GenericsTuts.G4_WildCards;
import java.util.*;
class GenericsBehaviours{
<T> void sampleMethodWC(List<? extends T> tempList){}
<T> void sampleMethod(List<T> tempList){}
}
public class G5_WildCardsBehaviours {
public static void main(String[] args) {
List<Integer> integerList = new ArrayList<>();
// First Case, no error, I understand how this works, by applying the type erasure rules, compiler replaces the type parameter T with the bound class Number, thus it accepts the child class
obj.<Number>sampleMethodWC(integerList);
// Second Case, compiler error
obj.<Number>sampleMethod(integerList);
}
}
I understand how the code works in the first case. However, why does not the code work in the second case? How does the compiler apply the Type Erasure rules in this case? Do they replace the T with something else other than Object or Number?