Reputation: 1081
In case of Function Overloading in C++, we know that there may occur ambiguity in the terms of actual and formal parameter mismatch. So there is a mechanism for resolve this.
For every actual parameter P, Si be the set of corresponding formal parameters that matches best. then S will be the intersection of all Si.
And for this there are 4 types of rules.
Does the similar process occur for Java too ? As the rules of function overloading is same in case of Java those types of ambiguity can take place here too.
Upvotes: 0
Views: 341
Reputation: 159086
Similar process? Well, depends what "similar" means.
It is however well documented in the Java Language Specification,
chapter 15.12 Method Invocation Expressions
and especially sub-chapter 15.12.2. Compile-Time Step 2: Determine Method Signature.
The process of determining applicability begins by determining the potentially applicable methods (§15.12.2.1).
The remainder of the process is split into three phases, to ensure compatibility with versions of the Java programming language prior to Java SE 5.0. The phases are:
The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.
The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase.
The third phase (§15.12.2.4) allows overloading to be combined with variable arity methods, boxing, and unboxing.
Deciding whether a method is applicable will, in the case of generic methods (§8.4.4), require an analysis of the type arguments. Type arguments may be passed explicitly or implicitly. If they are passed implicitly, bounds of the type arguments must be inferred (§18 (Type Inference)) from the argument expressions.
If several applicable methods have been identified during one of the three phases of applicability testing, then the most specific one is chosen, as specified in section §15.12.2.5.
Chapter 15.12.2.5 Choosing the Most Specific Method summarizes itself pretty good:
The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.
Of course, it is a bit more complicated than that.
Upvotes: 3