• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with overloading

 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its happening because of overloading. if you change
NewBird rap = new Raptor(); to Raptor rap = new Raptor();
doSomething() method of Class Raptor getting called.
 
Pandharinath Barve
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup i had seen that earlier
thanks for pointing it out
 
Heru Salim
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here is the core part, adding to Keith's point.



The above code self explanatory. and focuses the needed.
 
Heru Salim
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic