• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method overloading.

 
uday shet
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
I get the compiler error for the following code as "reference to a is ambiguous". Could anyone point out why it's so? The overloaded methods a(float i) and a (double i) are supposed to be distinct . So then why does the compiler flag it as ambiguous?

class zz{
void a(float i){
System.out.println("in zz.a");}
}
public class zzz extends zz{
public static void main(String uday[])
{
zzz z = new zzz();
z.a(10.2f);
}
void a(double i)
{System.out.println("in zzz.a");
}
}
 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear uday,
what is happening in your code is that the compiler is getting confused which method to call...
By default decimal variables are double...but since you have mentioned it "10.2f"....still it tries to convert it and gives you the error..
check this code below this also gives the same error.....this will not happen in case of int since there is no way to distinguish ie byte
is not 10b or 10s ............
<pre>
class check
{
void a(String s)
{
System.out.println(s);
}
}
public class checkit extends check
{
public static void main(String uday[])
{
checkit z = new checkit();
z.a(null);
}
void a(StringBuffer sb)
{
System.out.println(null);
}
}
</pre>
hope it helps.....
Harpal
 
uday shet
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harpal,
Thanks for your reply. But my doubt still lingers. Do you mean to say that the compiler cannot differentiate an int and a double?
If the above code is changed as :
line 2: void a(int i)...
line 9: z.a(10)
line 11: void a(double i)
The compiler errors out even now.
Please help.
Thanks,
Uday.
 
P SOLAIAPPAN
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Uday,
Harpal is correct. You are trying to confuse the compiler by defining both the methods with floating point numbers(float & double both are floating point numbers). You change any of your method parameter into "int" then it will be ok.
solaiappan
 
uday shet
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Some more 'R&D' on this:
If you move the overloaded method a(double i) into the base class
then there is no compiler error. This compiler seems to be able to make out which method to call.
Doesn't this seem strange? Any clarifications about this will be appreciated.
Thanks,
Uday.
 
P SOLAIAPPAN
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Uday,
again me. If you supply "int" as input parameter , the compiler will widen it to float or double to match the parameter type. The problem in the code is that overriding is not done properly.
What is the fun in giving same name for the method as in super class without not interested in overriding the original method in super class.
The compiler error is due to serious voilation of overriding. The error will be detected either during compile time or during dynamic binding of method as per your parameter you pass it.
I hope now it is clear.
solaiappan

[This message has been edited by P SOLAIAPPAN (edited November 03, 2000).]
[This message has been edited by P SOLAIAPPAN (edited November 03, 2000).]
 
uday shet
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi SOLAIAPPAN,
Thanks for your clarifications.I get what you are saying.
But if the compiler implicitly converts an int parameter to floating-pt, then why does it compile without any error if the overloaded (overridden, as per your clarification) method a(double i) is moved into the base class?
Thanks,
Uday.
 
P SOLAIAPPAN
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Uday,
There is no overloading here. When you are interchanging your base class method to sub class and viceversa. You are now having
void a(float i)
{System.out.println("in zz.a");}
inside class zzz is it not. Now compiler is not confused, because of your code
zzz z = new zzz();
z.a(10.2f);
You have defined exactly what the compiler wanted i.e
since object is of type zzz there is no problem here.
solaiappan

 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soliappan and uday,
Sorry ...I missed all the discussion I was out for dinner....Uday ,hope the explanation given by soliappan must have made things clear...........
Harpal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic