• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Overloading

 
Phal Ach
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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.
 
Phal Ach
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



?
 
Akash Mohapatra
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
Phal Ach
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phalguni
Now I'm clear, thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic