Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

ambiguity error in method overloading

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i invoked the method show() with obj.show('A','B','C','D'); AND out of 1,2,3,4 only one is present at a time then it shows the output as mentioned in the comment. can anyone please tell me
the reason why its happening like this. i mean in two cases it is giving error and in two other cases it is not giving error.

void show(char a,char b, int c, int d){
System.out.println("(char,char,int,int)");
}

1) void show(int a,int b, int c,char d){
System.out.println("(int,int,int,char)");
}// it shows ambiguity error

2) void show(int a,int b, char c,int d){
System.out.println("(int,int,char,int)");
}// it shows ambiguity error

3) void show(char a,int b, int c,int d){
System.out.println("(char,int,int,int)");
}// no error compiles and void show(char a,char b, int c, int d) is invoked

4) void show(int a,char b, int c,char d){
System.out.println("(int,char,int,int)");
}// no error compiles and void show(char a,char b, int c, int d) is invoked
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where is method overloading happening ? you are saying out of 4 methods only one is present then how come overloading taking place ? Please be clear about the question so that other ranchers are able to understand what you want to convey
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, let me change your subject header -- your previous one just strikes me as too desperate. Not saying that you are, just mentioning that volunteers don't enjoying answering questions when there is a clock ticking (nor do they really care about that clock).


There are actually many reasons why a method is considered ambigious or not -- and most of them don't apply here. The one that does apply for this topic is related to the concept of "most specific" method. Here is the link to the Java Language Specification description of the rule...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5

There is a ton of legalese verberage, even in this simple case (ignoring all the variable arity portions), but the phrase that seems to summarize it best is this one...

Java Language Specification wrote: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 type error.



Anyway, I can elaborate, and write up an example later, if you like. In the meantime, I will let you read it and take a shot at understanding it. It is actually not that bad. It just looks like giberish at first.

Henry
 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also...

gurpeet singh wrote:where is method overloading happening ? you are saying out of 4 methods only one is present then how come overloading taking place ? Please be clear about the question so that other ranchers are able to understand what you want to convey



Agreed. The topic does seems confusing. Either due to bad formatting or bad wording. However, this is what I gathered.

There is actually five methods. One method, and optionally, another method, which is chosen from four possible methods. So, only two methods are being overloaded at any time. The call will call the methods with four char parameters, which works with all five methods (in insolation). In the specified pairs, however, you get the description mention in the topic.

Hope this helps,
Henry
 
abhinas raj
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for the inconvenience due to bad formatting .. here i am writting in a more readable form . actually this question is not from any book .... while analysing the problems on method overloading i developed this problem. i observed the output in all four cases (1,2,3,4) (each case contains a seperate method) .

class Overload {
public static void main(Strings args[]){

MethodOverload obj=new MethodOverload();

}
}

class MethodOverload {


// this method is common for all the four methods mentioned bellow (1,2,3,4) .
void show(char a,char b, int c, int d){
System.out.println("(char,char,int,int)");
}


// out of the following four methods (1,2,3,4) only one will be present at a time for method overloading.

1) void show(int a,int b, int c,char d){
System.out.println("(int,int,int,char)");
}// it shows ambiguity error

2) void show(int a,int b, char c,int d){
System.out.println("(int,int,char,int)");
}// it shows ambiguity error

3) void show(char a,int b, int c,int d){
System.out.println("(char,int,int,int)");
}// no error compiles and void show(char a,char b, int c, int d) is invoked

4) void show(int a,char b, int c,char d){
System.out.println("(int,char,int,int)");
}// no error compiles and void show(char a,char b, int c, int d) is invoked

}


when i invoked the method show() with obj.show('A','B','C','D'); AND out of 1,2,3,4 only one is present at a time then it shows the output as mentioned in the comment. here out of four methods in case of two
methods (1 and 2 ) it is not giving any error it prints the output. but in the case of other two methods (3 and 4) it give error.

then i have to ask that what is the logic behind this due to which in two cases it is giving error(3 and 4) and in two other cases it is not giving error (1 and 2).

 
abhinas raj
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:First, let me change your subject header -- your previous one just strikes me as too desperate. Not saying that you are, just mentioning that volunteers don't enjoying answering questions when there is a clock ticking (nor do they really care about that clock).


There are actually many reasons why a method is considered ambigious or not -- and most of them don't apply here. The one that does apply for this topic is related to the concept of "most specific" method. Here is the link to the Java Language Specification description of the rule...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5

There is a ton of legalese verberage, even in this simple case (ignoring all the variable arity portions), but the phrase that seems to summarize it best is this one...

Java Language Specification wrote: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 type error.



Anyway, I can elaborate, and write up an example later, if you like. In the meantime, I will let you read it and take a shot at understanding it. It is actually not that bad. It just looks like giberish at first.

Henry




sir please write an example and explain that for me ,i read the link , i coudnt understand properly . it was looking like i am studying mathematics.
 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abhinas raj wrote:
sir please write an example and explain that for me ,i read the link , i coudnt understand properly . it was looking like i am studying mathematics.



The *math* really isn't that bad.... First, more than two thirds of it are related to var-args. Second, of the remaining stuff, about two thirds of that are related to generics. All that remains are a couple of expressions, and a few paragraphs explaining it. Definitely worth trying again -- especially since you will likely visit it again, in the future, for generics and var-args.

[EDIT: I am only partially serious about the second part. It seems, in my opinion, that this section is so complex that implementors don't seem to get it right -- meaning that there seems to be lots of compiler bugs regarding overloading and var-args. And that what compiles with the Sun compiler doesn't compile with the other compilers, and vice versa. In summary, definitely worth figuring out -- but I would avoid the var-args portion for now.]

Henry
 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, let see if I can explain it without the "math" stuff... and start from the summary...

Java Language Specification wrote: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 type error.



The phrase "any invocation handled by the first method could be passed on to the other one without a compile-time type error" means that the parameters can be converted only by implicit casting. Take this very simple example....



In this case, if we try to pass a (char) to both methods, it will work. This means that the second method can handle any invocation handled by the first. However, if we try to pass a (int) to both methods, the first will not work (can't pass an int to method expecting a char without explicit casting). The first method cannot handle any invocation handled by the second method. So, what does this mean? Well, it means that the first method is more specific than the second method..... and as for what do we do with this conclusion, we will get to it.


Anyway, let's try this again with your common and third method...



If we try to pass a (char, char, int, int) to both methods, it will work. This means that the second method can handle any invocation handled by the first. However, if we try to pass a (char, int, int, int) to both methods, the first will not work (regarding the second parameter, can't pass an int to method expecting a char without explicit casting). The first method cannot handle any invocation handled by the second method. So, what does this mean? Well, it means that the first method is more specific than the second method.

Later, when your main test code calls these two methods with a (char, char, char, char) it will actually work for both methods -- and since the first method is more specific than the second, the compiler will chose the first method to call.


Now, let's try this again with your common and first method....



If we try to pass a (char, char, int, int) to both methods, the second will not work (same explanation regarding the fourth parameter). This means that the second method cannot handle any invocation handled by the first method. Also, if we try to pass a (int, int, int, char) to both methods, the first will not work (same explanation regarding the first and second parameter). The first method cannot handle any invocation handled by the second method. So, what does this mean? Well, basically these tests failed, and that neither method can be deemed as more specific than the other.

Later, when your main test code calls these two methods with a (char, char, char, char) it will actually work for both methods -- and since there isn't any rule that the compiler can use to determine which method to use, it will generate a compile error.


As for the second and fourth method overloads with the common method, you can probably work that out yourself.

Hope this helps,
Henry
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to Henry's exemplary explanation, you can refer to this - http://radio.javaranch.com/corey/2004/08/19/1092931618000.html
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The types of the parameters of the first member method are T1, ..., Tn-1, Tn[].

The types of the parameters of the other method are U1, ..., Uk-1, Uk[].

If the second method is generic then let R1 ... Rp (p ≥ 1) be its type parameters, let Bl be the declared bound of Rl (1 ≤ l ≤ p), let A1 ... Ap be the type arguments inferred (§15.12.2.7) for this invocation under the initial constraints Ti << Ui (1 ≤ i ≤ k-1) and Ti << Uk (k ≤ i ≤ n), and let Si = Ui[R1=A1,...,Rp=Ap] (1 ≤ i ≤ k).



not able to understand what the following equation says, can someone give me the idea.

Ti << Uk (k ≤ i ≤ n), and let Si = Ui[R1=A1,...,Rp=Ap] (1 ≤ i ≤ k).

 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sakthi moorthy wrote:
not able to understand what the following equation says, can someone give me the idea.

Ti << Uk (k ≤ i ≤ n), and let Si = Ui[R1=A1,...,Rp=Ap] (1 ≤ i ≤ k).



Can you elaborate on what you are confused with? That quote is (1) related to var-args, (2) related to generics, of course, (3) related to overloading, and (4) is not really in context (lots of other stuff applies). In other words, you are asking about a very complicated part of the JLS -- any clarification would help, and even then, I wouldn't expect a quick answer.

Henry
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic