• Post Reply Bookmark Topic Watch Topic
  • New Topic

Function Overloading  RSS feed

 
Siddhant Agarwal
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I declared two functions in a class like this:


Case 1:
Only Type A and Type B are defined in my class
a. It compile successfully.

b. When I call function like this: or It calls the Type B implementation.

c. When I call function like this: It shows me a compile time error.

Case 2:
All three types are defined in my class -> I get a compile time error saying that func2 is already defined.

Firstly, why is this difference in behavior observed between Case 1 and Case 2? When we had two functions(Type A and Type B) where the only difference was the return type, it compiled. However, when we added Type 3, again with a different return type, it didn't compile?

Secondly, between Case1.b and Case1.c, on what basis does Java decide when to call which implementation of the function (between Type A and Type B)
 
O Shea
Ranch Hand
Posts: 196
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all Case 1->

When defining only TYPE A and TYPE B and commenting TYPE C then-> Immediately you get Compile time error saying that Func1 is duplicate method.

Since function overloading means that there is only change in the list/order of arguments . Whether you change access modifier or return type, it does not matter . What matter in Overloading is only change in number/order of arguments.

Now see in Case 1-> You have 2 functions and . See here both the functions have same number/order of arguments , there is no change that's why it does not compile.

Examples of Overloaded functions->

1-> public void Func1(){}

2->private int Func1(int a){}

3->protected double Func1(String s){}

4->float Func1(int a,String s){}   //See order changes here

5->float Func1(String s,int a){}  //See order changes here

See above there is change in number/order of arguments that's why they are overloaded.

Now when you uncomment TYPE C then it does not give Compile time error since the whole function changes , now it is not overloading because the name of function changes from Func1 to Func2.

Not-> Functions are said to be overloaded when there is only change in number/order of arguments and name of function should be same. Change in return type or access modifier dosen't have any impact on Function Overloading.
 
Siddhant Agarwal
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh. So, it has to be a difference in the list or type of parameters for overloading to be valid. For a change in modifier, access specifier, return type or anything else in the method signature won't make a difference.
Thanks for the clarification!

Sorry for the late reply.
 
Matt Wong
Greenhorn
Posts: 23
3
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: Some obfuscators use such "false overloading" so when decompiled the re-generated source isn't correctly compileable anymore.
Why? Cause such "false overloading" is permitted at bytecode level cause method calling works differently down there. But that's nothing a normal developer has to worry about. These are some low-level tricks when you want to protect the compiled class file.

btw: in Java it's called "method"
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!