3

The book I am reading on Java states something confusing and unacceptable.

Learning About Ambiguity

When you overload methods, you risk creating an ambiguous situation - one which the compiler cannot determine which method to use. For example, consider the following overloaded computeBalance() method declarations:

public static void computeBalance(double deposit)
public static void computeBalance(double withdrawal)

If you declare a double variable named myDeposit and make a method call such as computeBalance(myDeposit);, you will have created an ambiguous situation. Both methods are exact matches for your call. You might argue that a call using a variable named myDeposit "seems" like it should go to the version of the method with the parameter named deposit, but Java makes no assumptions based on variable names. Each version of computeBalance() could accept a double, and Java does not presume which one you intend to use.

This violates the rules for overloading a method. How can a method be overloaded with the same parameter list? Isn't it impossible or am I not getting something? I have tried and compiled such a code, it returns the following error (which makes sense):

method computeBalance() is already defined in class XXX
  • 3
    Read the paragraph again. It says that you cannot have method overloading with same parameter list. – Her Majesty Queen of ARC Nov 14 '15 at 14:37
  • @DalijaPrasnikar, actually, I don't see where the quoted text says that: it does not use the word "list", let alone the phrase "same parameter list". – Erik Eidt Nov 14 '15 at 19:36
  • He said "exact matches for your call" and in Java that translates to parameter list. – Her Majesty Queen of ARC Nov 15 '15 at 12:46
5

I agree with your assessment that this is poor, wrong and/or misleading text.

As you have pointed out, the compiler will complain based on these declarations alone: no call to computeBalance is required. These declarations taken together are in error, there is no (legal) overloading going on here.

The idea from the text that

You might argue that a call using a variable named myDeposit seems like it should go to the version of the method with the parameter named deposit ...

is an odd suggestion to say the least. The author appears to be evoking a hypothetical situation (variable name matching (to resolve erroneously declared overloads)) that isn't very apropos of how these languages work, but then goes on to say they don't work like that. Silly if you ask me.

For what it's worth, there are much better examples of ambiguous overloads that could be presented.

Two methods with the same name each taking one parameter of a different (class) type. Then you call that method name passing null. The compiler will not know which of the overloads to invoke: you'd have to cast null to one of those types.

0

Obviously two methods in example are ambiguous and that code cannot be compiled because both methods have same name and same parameters.

public static void computeBalance(double deposit)
public static void computeBalance(double withdrawal)

Author does not claim that you can compile and use above as stated in

When you overload methods, you risk creating an ambiguous situation - one which the compiler cannot determine which method to use.

and then

and make a method call such as computeBalance(myDeposit);, you will have created an ambiguous situation. Both methods are exact matches for your call.

and later in

but Java makes no assumptions based on variable names. Each version of computeBalance() could accept a double, and Java does not presume which one you intend to use.

The only thing author didn't explicitly say in text you quoted is that ambiguous code cannot be compiled.

Personally, I don't think that is mistake on his part and that his text is in any way misleading or confusing, because that should be obvious from "compiler cannot determine which method to use" at least.

Author is just creating hypothetical situation where person reading the code might be able to determine right method based on variable and parameter names, trying to explain that compiler has no such abilities and cannot see broader context.


Just as additional note. Different languages have different definitions what represents ambiguous call. For instance, while Java compiler is not able to overload methods that have same parameter list but different return type Swift compiler can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.