javaoverloadinggeneric-method

Overloading method with different parameterized type parameter


List<Number> and List<Integer> are different type: if that is the case why overloading the following method does not compile?

public static  void iMethod(List<Number> nList){
    System.out.println(nList);
}


public static  void iMethod(List<Integer> iList){
    System.out.println(iList);
}

error:

Erasure of iMethod<List<integer>> is same as another method in class


Solution

  • Java has this thing called "type erasure" which causes problems like this when compiling generic code like this. When compiled, List<Number> and List<Integer> are both just plain List.