1

These two functions in the same class with identical names does not cause an error, because the input variable types are different. (String and int)

public static int sameName(HashMap<Integer, String> _map, String _var) {
    return 42;
}

public static int sameName(HashMap<Integer, String> _map, int _var) {
    return 42;
}

In this case, the variable types are also different, still this causes an error. The first one uses a HashMap<Integer, String>, the second uses HashMap<Integer, Integer>.

public static int sameName(HashMap<Integer, String> _map, int _var) {
    return 42;
}

public static int sameName(HashMap<Integer, Integer> _map, int _var) {
    return 42;
}

Why is this? Apart from choosing a different function name and flipping the order of variables, are there any more professional ways to solve this, without manipulating the consistency of the names of my functions?

user43968
  • 2,049
  • 20
  • 37
Gábor DANI
  • 2,063
  • 2
  • 22
  • 40

1 Answers1

4

Generics are erased after compilation, so the signatures of both methods is just:

int sameName(HashMap, int)

Change "sameName" to something more meaningful if the methods really are doing two different things.

Riaz
  • 874
  • 6
  • 15