Duplicate Method while Method-Overloading

社会主义新天地 提交于 2019-12-07 05:27:13

问题


Following code gives compilation error with error "Duplicate Method"

static int test(int i){
     return 1;
}

static String test(int i){
     return "abc";
}

This is expected as both the overloaded method have same signature and differ only in return type.

But the following code compiles fine with a warning:

static int test1(List<Integer> l){
    return 1;
}

static String test1(List<String> l){
    return "abc";
}

As, we know the Java Generics works on Erasure, which mean in byte-code, both these method have exactly the same signature and differs with return type.

Furthur, to my surprise the following code again gives compilation error:

static int test1(List<Integer> l){
    return 1;
}

static String test1(List l){
    return "abc";
}

How is the second code working fine without giving any compilation error, though there is duplicate method?


回答1:


Resolving overloaded methods is done at compile-time, rather than runtime, so the Java compiler would know the difference between the two in your second example. In your third example, the problem is that a List<Integer> is also a List, so it wouldn't know which one to use if you passed in a List<Integer>.




回答2:


  1. Java can not determine which one to use if the parameters are the same. So, it throws a duplicate method error instead.
  2. List of String and List of Integer are not directly conversible, so the methods are different. No error.
  3. List of Integer can also be used as a plain List of anything, so Java can't determine which one to use if supplied a List of Integer -> duplicate method error.



回答3:


First result is expected and correct.

Now lets talk of 2nd:

Lets try calling those functions.

List<Integer> intList;
List<String> strList;
test1(intList)
test1(strList)

Compiler will call respective methods.

Now 3rd

List<Integer> intList;
List unknownList;
test1(intList);
test1(unknownList);

Woah!! which method should compiler call now!! 'cause unknowList can be a List of Integers. Hope it helps.




回答4:


Essential changes for method overloading : PARAMETERs must be different. In your case both methods test uses the same parameters all the time. Secondly Return types may or may not change.Instead try this for first example.

static int test(int i){
return 1;
}

static String test(List l){
return "abc";
}

Your second example works because two List<String> and List<Integer> are two different parameters.

Third example fails because List of Integer can also be used as a List.After the code runs if a List integer is passed as parameter, Java wont be able to determine which one to of the two functions should be called.



来源:https://stackoverflow.com/questions/16377949/duplicate-method-while-method-overloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!