• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method overloading in the derived class

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody,
I was trying something out and got confused on something basic.
I have a Base class which has methods -
getVar(int)
getVar(char)
and Derived class which overloads it -
getVar(float)
The compiler gives ambuguity error for getVar(int) and getVar(char)
Instead, if I switch the methods, so that -
Base class has -
getVar(float) and
Derived class has -
getVar(int)
getVar(char)
It works !! In this case there is no ambiguity error.
I don't know the reason behind it.I would appreciate if somebody can explain it to me.
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Celina, the ambiguity should be between getVar(int) and getVar(float) isn't it? guess there was a typo.
neways, during method invocation, the object instance is implictly passed in the method call.
so the methods can be looked at like this...
getVar(Base b, int i);
getVar(Base b, char c);
getVar(Derived d, float f);
Now, if there are 2 methods and all the parameters of method-1 can be passed to method-2 without the need to do an explicit cast, then method-1 is said to more specific than method-2.
if we examine this snippet -
Derived d = new Derived();
d.getVar(1);
We have 2 candidates for sending the call.
1 - getVar(Base b, int i)
2 - getVar(Derived d, float f)
getVar(char) method is immediately rejected.
considering method-1, the 1st argument, Base of method-1 cannot be passed to Derived of method-2 unless a cast is used. Therefore method-1 is not more specific.
considering method-2, the 2nd argument, float of method-2 cannot be passed to int of method-1 unless a cast is used.
Therefore, neither is more specific and hence the ambiguity.
If the methods getVar(float) and getVar(int) are swapped, then ambiguity no longer exists. I'll leave the working to you.
Hope this helps.
 
Celina Joseph
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vin,
Thanks for the reply. But I am a little confused at the following -
In the above snippet,
1- getVar(Base b, int i) is not specific
But I do not understand why the compiler cannot pass the int value in
d.getVar(1)
to the method -
2- getVar(Derived d,float f)
An int can be assigned to a float without getting any value lost and this casting can be implicitly done by the compiler.
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Celina, Your second post is "more specific" than your first post. But i'm afraid i cannot be
"more specific" than mine. Please work through it again.
There are two choices for method invocation - getVar(int) and getVar(float) - When java tries to determine the most specific method, it fails due to ambiguity because of the Types.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler uses context to figure out what you mean. In the case of assigning a float to an int, the context is clear and the only way to perform the operation is through an implicit cast. However, when you call a function and there are two alternatives, it won't make an implicit cast unless it is necessary. Even then, it follows a specific set of rules. Let me show with an example:
d.getVar(1)
Since 1 has type int, this will call the version that takes an int parameter.
d.getVar(1.0)
In this case, 1.0 has type float, so this will call the version of the method that has a float parameter.
Data types are very specific and there is no implicit conversion applied unless the compiler absolutely has to do it.
Since a char is simply stored as an integer value, it seems possible for ambiguity between char and int. This is unlikely, though, especially if you are dealing with a char literal. I don't understand why the compiler would give you this ambiguity error. It will help a lot if you post the line of code with the function call. It will help to see why the compiler would give that error.
HTH
Layne
 
Celina Joseph
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I need to read the most specific resolution algorithm used in Java to actually understand what's happening. It would be great if somebody can suggest the best place for it.
Appreciate it.
[ October 09, 2002: Message edited by: Celina Joseph ]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi celina
i remember this dicussion took place when i was active in SCJP forum. i'll try to find and give the link to u...
it had finally...almost clear explanation...
but here what i remember as flow of resolution,
1. first the declared class is checked for the method
2. if the first class "fails" then the parent methods that were derived in child are traversed
3.also the following thing we need to keep in mind,
"there is a implicit casting occuring by java compiler" and the "fial" word in 2nd rule will mean that the compiler couldn't find ANY method, **even after applying implicit cast**, that matches the call.
4. if there is a "direct(/perfect)" match in the declaring class of the object IT IS APPLIED w/o any other consideration.
these points solves ur puzzle i guess because, say u 've following code,

now here as 'c' has been 'declared' as Child object when we call c.get(2) is goes an searches the method space for Child class (meaning only those methods which are Child's declared methods) (Rule-1 applied)
now as of combination of Rule-2&3 it tries to see if it can cast 2 to float. IT CAN cast. the compiler records this as say event-1.
compiler also sees that there is method get() from Parent which can be applied here. so say that is noted down as event-2.
so.....now THE COMPILER is confused (amongst event-1 and event-2 results) because it has two options when the code will run-
1) to use Child's get() or
2) to use Parent's get() that was derived
BUT if u reverse the order(Child having argument int and Parent having float) than also we will get two events but as of Rule-4 it determines to call Child's method and it doesn't give us compilation error.
u can try it.
hope this helps.
regards
maulin
PS: btw, i gave the explanation here so...i may not try to find that link i was talking about but i guess that is okay...u can search in SCJP forum for my name (may be u'll find it)...
 
Everybody's invited. Even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic