This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem with overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem with overloading" Watch "problem with overloading" New topic
Author

problem with overloading

Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
public class NewBird {

public static void main(String[] args) {
NewBird rap = new Raptor();
rap.doSomething(1); //a
rap.doSomething(1,2);
}

public void doSomething(int i, int ...args) {
System.out.print("Hallo ");
}

public void doSomething(double d, int i) {
System.out.print("Ola");
}
}

class Raptor extends NewBird {
public void doSomething(int n){
System.out.print("Ciao");
}
}

the output is HalloOla

i just want to confirm that the output is due to over loading of the doSomething() method.

am i right ?
Pandharinath Barve
Greenhorn

Joined: Aug 04, 2006
Posts: 13
its happening because of overloading. if you change
NewBird rap = new Raptor(); to Raptor rap = new Raptor();
doSomething() method of Class Raptor getting called.


With Best Regards,<br />Pandharinath.<br />SCJP 5.0
Pandharinath Barve
Greenhorn

Joined: Aug 04, 2006
Posts: 13
its happening because of overloading. if you change
NewBird rap = new Raptor(); to Raptor rap = new Raptor();
doSomething() method of Class Raptor getting called.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

public class NewBird {

public static void main(String[] args) {
NewBird rap = new Raptor();
rap.doSomething(1); //a
rap.doSomething(1,2);
}

public void doSomething(int i, int ...args) {
System.out.print("Hallo ");
}

public void doSomething(double d, int i) {
System.out.print("Ola");
}
}

class Raptor extends NewBird {
public void doSomething(int n){
System.out.print("Ciao");
}
}

the output is HalloOla

i just want to confirm that the output is due to over loading of the doSomething() method.

am i right ?


Hi Prasad, Dont you see another thing that is happening with your code.
You passed two int's and the doSomething(double,int) is called instead of
doSomething(int,int...);

Preference is given to (double,int) version of doSomething() instead of var argument one.
"Preference is given to widening (int to double) instead of var arg"

Regards,
cmbhatt
[ March 27, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
yup i had seen that earlier
thanks for pointing it out
Heru Salim
Greenhorn

Joined: Mar 27, 2007
Posts: 4
Hi Prasad, Dont you see another thing that is happening with your code.
You passed two int's and the doSomething(double,int) is called instead of
doSomething(int,int...);

Preference is given to (double,int) version of doSomething() instead of var argument one.
"Preference is given to widening (int to double) instead of var arg"

Regards,
cmbhatt

[ March 27, 2007: Message edited by: Chandra Bhatt ][/QB]

Hi,

I don't think it's not the only the cause. It only explain the second call "doSomething(1,2)", but doesn't explain the call of "doSomething(1)".

I thought previously that the output should be "CiaoOla" (regarding to polymorphism concept), but I was wrong.

I think the problem is that "doSomething(int, int...)" has a higher priority than "doSomething(int)", when it's called "doSomething(1)".

Don't know exactly about that, but I think that's the reason I found out.

Regards,
Heru
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
No, it doesn't have a higher priority.

The reason that doSomething(int,int...) is called when the method invocation is doSomething(int) is that it matches the call.

doSomething(double,int) does not match doSomething(int)
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi,
Here is the core part, adding to Keith's point.



The above code self explanatory. and focuses the needed.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Heru Salim
Greenhorn

Joined: Mar 27, 2007
Posts: 4
Originally posted by Keith Lynn:
No, it doesn't have a higher priority.

The reason that doSomething(int,int...) is called when the method invocation is doSomething(int) is that it matches the call.

doSomething(double,int) does not match doSomething(int)


Maybe I misunderstood.
But I didn't compare NewBird.doSomething(int, int...) with NewBird.doSomething(double, int). I compared instead NewBird.doSomething(int, int...) with Raptor.doSomething(int), which both could represent the calls of rap.doSomething(1).

As an example if I change the signature of NewBird.doSomething(int, int...) into NewBird.doSomething(int), the Raptor.doSomething(int) will be called, when it's called via rap.doSomething(1).

Hope that my explanation about my opinion could be understood.

Heru
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Maybe I misunderstood what you were saying. I'm sorry about that.

In order to make the JDK compatible with older versions, method matching is done in phases.

The first phase is to check and see if there are any matches without allowing boxing or varargs.

If there aren't any, then the second phase is to see if there is a match allowing boxing but not varargs.

If there aren't any, then the third phase is to see if there are any matches allowing both boxing and varargs.

In this case, in Raptor doSomething(int) will be matched before doSomething(int,int...).
Heru Salim
Greenhorn

Joined: Mar 27, 2007
Posts: 4
Hi Keith,

just guessing, that you didn't try the code from Prasad, did you?
That was also my guess, that Raptor.doSomething(int) preceed NewBird.doSomething(int, int...), when it's called by



That's why I was also surprised and guessed that signature method(int, int...) has a higher priority than method(int) when called with method(1).

It is also new for me, and have no idea about the real reason.

Heru
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The things that I stated are true in a situation where there are two methods that could potentially match a call.

In the case of this particular code, the object is of type Raptor, but the reference is of type NewBird.

When you call a method on the reference, only the methods that can be seen by a NewBird reference can be considered.

In this case, the only method in NewBird that matches the call is doSomething(int,int..).

If you changed the reference type to Raptor, then you would see that doSomething(int) in Raptor would match instead of doSomething(int,int...).
[ March 27, 2007: Message edited by: Keith Lynn ]
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Heru,

I got you in the first mail in your first mail.
Considering Prasad Example here.



Here pay attention to where the Raptor Instance stored? (which is NewBird reference "rap")
NewBird reference rap can only call methods defined in itself. "rap" can't call Raptor class's method, though "rap" pointing to Raptor Object, unless until any method overriding done in Raptor.

Hope its clear now.
Heru Salim
Greenhorn

Joined: Mar 27, 2007
Posts: 4
Originally posted by Srinivasan thoyyeti:
Hi Heru,

I got you in the first mail in your first mail.
Considering Prasad Example here.



Here pay attention to where the Raptor Instance stored? (which is NewBird reference "rap")
NewBird reference rap can only call methods defined in itself. "rap" can't call Raptor class's method, though "rap" pointing to Raptor Object, unless until any method overriding done in Raptor.

Hope its clear now.


Ah ha...
Now i get it. It just like rap has no interface to access doSomething(int), so it calls doSomething(int, int...) instead, which match to the calls rap.doSomething(1).

Thanks for the explanation.

Heru
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Srinivasan,

Yeah, Out of dozen postings, your explanation solved Heru's puzzle.


But in first sight, one may easily overlook this detail; that is "what can only be called using the parent class reference variable?" Parent class only knows what it is having, and nice game of polymorphism is played around this only by the JVM. To call child specific method we need explicit casting and ofcourse not to forget the instanceof test before doing do; "confirming if the reference really points to child class object"


Thanks & Regards,
cmbhatt
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: problem with overloading
 
Similar Threads
Static initializer...
Sub class doubt
confusion with output...
Please explian the output ....
Mock exam inheritance/varargs question