9.11 FUNCTION OVERLOAD RESOLUTION IN JAVA


9.11 FUNCTION OVERLOAD RESOLUTION IN JAVA

As in C++, overload resolution in Java is based on the notion of match specificity. If there is more than one method whose parameters match the arguments in a function call, the compiler selects that method which is maximally specific. If more than one method is found to be maximally specific, the compiler declares ambiguity and puts out a compile-time error message to that effect.

So while the overall approach remains the same as described in the last section for C++, the algorithmic details are different for the following reasons:

  • As already mentioned in Section 6.7, Java is more strongly typed than C++; Java does not permit many of the type conversions that are allowed in C++. In fact, in function invocation, Java allows only the widening kind of type conversions discussed earlier in Chapter 6 for the primitive types and the widening kind of type conversions for the class types, these being from a derived class to a superclass or a superinterface. Allowing only widening kind of type conversions in function invocations eliminates one of the specificity levels in C++—the one corresponding roughly to standard conversions.

  • Since Java does not allow user-defined type conversions the way C++ does, that eliminates another level of specificity.

  • Since there is no ellipsis in Java, that eliminates yet another level of specificity.

These reasons and the fact that, compared to C++, Java is more modern and has had the benefit of hindsight in its design, have led to the following three-step procedure for overload resolution in Java. There are reproduced here from [2].

  1. For a given function call, find the set of all applicable methods on the basis that there must exist either an exact match or a widening type conversion from each argument in the function call to the corresponding parameter of the method being considered. If this set consists of only one method, select that method for invocation. Otherwise proceed to the next step.

  2. If there exists either an exact match or a widening type conversion from each of the parameters of a method in the set to the corresponding parameters of another method, eliminate the latter method from the set. If you are left with only one method after pruning the set in this manner, select that method for invocation. Otherwise proceed to the next step.

  3. Declare the source code invalid because there does not exist a single maximally specific method that can be invoked for the function call.

Consider the following example:

 
//Overload.Java class Employee {String name;} class Manager extends Employee {int level;} class Test { static void foo(Employee e1, Employee e2) { //first foo //(A) System.out.println( "first foo"); } static void foo(Employee e, Manager m) { //second foo //(B) System.out.println( "second foo"); } static void foo(Manager m, Employee e) { //third foo //(C) System.out.println( "third foo"); } public static void main(String[] args) { Employee emp = new Employee (); Manager man = new Manager(); foo(emp, man); // will invoke the second foo //(D) //foo(man, man); // Error because it produces an //(E) // ambiguity in overload resolution } }

The argument types in the function call in line (D) match the parameter types in the first and the second definitions of foo in lines (A) and (B). So at the end of the first step of overload resolution, we have two candidate methods in the set. We therefore proceed to the second step and discover that both the parameters of the second definition of foo in line (B) can be matched via widening conversions with the corresponding parameters of the first definition of foo in line (A). This means that the second foo is more specific than the first foo. We therefore drop from the set the first foo. That leaves us with only one method in the set. The compiler selects this method.

Now consider that happens if we uncomment the call to foo in line (E). Its argument types match the parameter types for all three definitions of foo. So at the end of the first step of overload resolution, we have three candidates in the set. By the same reasoning as before, we now discover that the first definition of foo is less specific than the other two. So we delete it from the set. However, we also discover that it is not possible to delete either of the remaining two entries from the set, for the simple reason that an Employee type does not possess a widening type conversion to a Manager type. So we declare ambiguity and, therefore, compile time error results.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net