The algorithm used by the compiler for the resolution of overloaded methods incorporates the following phases:When I first read on this, I never know such rules ever exist. Does this only happen to JAVA or it have been there since object oriented is invented? Anyhow, do not overlook on the rules mention above, there are priority when resolving overloading method. The condition define in first rule will always verify first before second and third rule is verify. If it fails, compiler will move on to the next rule, until it found a match. Consider following situation:
- It first performs overload resolution without permitting boxing, unboxing, or the use of a varargs call.
- If phase (1) fails, it performs overload resolution allowing boxing and unboxing, but excluding the use of a varargs call.
- If phase (2) fails, it performs overload resolution combining a varargs call, boxing, and unboxing.
public static void funcA(String str, Object... obj) { // (1) ... } public static void funcA(String str, Integer... i) { // (2) ... }I have 2 overloading method at above code, if I do this:
funcA(“STRING”, 10, 10);
Compiler resolved the overloading method easily by picking up method (2). Now if I change this:
public static void funcA(String str, int x, Object... obj) { // (1) ... } public static void funcA(String str, Integer... i) { // (2) ... }This would bring trouble to the compiler as both overloading method could be the best fit, compiler unable to identify the most specific overloading method, thus compiler were force to issue an compile error to me.
No comments:
Post a Comment