aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Overloading" Watch "Method Overloading" New topic
Author

Method Overloading

Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
Hi.Below is the code from www.danchisholm.net of some mock test.

class GFC202 {} class GFC203 extends GFC202 {}
class GFC204 {
static void m(GFC202 x) {System.out.print("GFC202");}
static void m(GFC203 x) {System.out.print("GFC203");}
public static void main(String[] args) {m(null);}
}

It prints GFC203. But I don't understand how does it think method m(GFC203 x) to be more specific than GFC202? Please explain. Thanks
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52



If you look at the above code you will notice that a method with int argument is called. Where as this would have been a valid call to the other method with long argument as well. But compiler will chose the narrower( or more specific) argument method.

Your code is analogous to the above code. A sub class is always considered more specific to a super class. Hence in your code the method with subclass as argument was called.


Still Learing..
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
Hi Jaspreet

It means in my case class GFC203 was nearest. So it was taken as a more specific one. I think it makes sense to me. Thank you very much.
Saurabh Vyas
Ranch Hand

Joined: Sep 02, 2003
Posts: 72
In Case of Jaspreet's Example, it is clear that int is narrower than long so it got preference by the compiler.

But in Case of Phalguni's Example, How can we conclude that GFC203 was narrower than GFC202 ??

Is it that since GFC203 is a child class to GFC202 and thus it was being considered as a more specific class (& thus more narrower) as compared to it Generic Parent class, and thus got preference of selection by the Compiler !!!

Can anyone please Explain the behavior in detail.

Thanks
Saurabh
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Well GFC203 is the narrowest. I can see how the compiler has to use this to make a choice, but how does the compiler determine the following:



?


Java hobbyist.
Akash Mohapatra
Greenhorn

Joined: Aug 20, 2007
Posts: 17
@Ronald,
In this case, the compiler will not be able to determine which method to be called since both are equally eligible for the given method call.

Hence it will throw a compile-time error.


SCJP 6.0, SCWCD 1.5
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
In Jaspreet's example, int is the most obvoious and clear option. But it seems that in such ambiguous situation, if a condition mathes the nearest subclass, that will be selected first by the compiler, and if not direct subclass, then its superclass will be matched for condition. If arguements matches any two classes, then it is ambiguous for compiler and generates compiler error. Please correct me if I am wrong. Thanks
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
As I already said, A sub class is always considered more specific to a super class. Hence in your code the method with subclass as argument was called.
kishor kalapa
Greenhorn

Joined: Jun 05, 2008
Posts: 24
code:
class MyClass{
public void method(int i){ System.out.println("int");} //1
public void method(long i){ System.out.println("long");}//2

public static void main(String... args){
MyClass my = new MyClass();
my.method(x);
}
}
----------------------------------------------
In above code what is the x value where I will get out put as "long"
I'm try by x=9L, it working I got "long" .
But what is the value if I given to x(with out suffix L) I will get out put as "long"
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
Hi Kishor

In your case, x won't be any out of blue value. It will be stored in either int type or long type variable. So it will find its best suitable method and execute. If your variable x is like long x = 111L; or long x=111; it will execute method with long only because your x is of type long. But in your method myMethod(x), instead of x if you will put some value without any suffix, it will go to int. And if you add suffix L it will go to long. I hope I understood your point properly.
kishor kalapa
Greenhorn

Joined: Jun 05, 2008
Posts: 24
Hi Phalguni
Now I'm clear, thanks.
 
wood burning stoves
 
subject: Method Overloading