Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Advantage of MethodOverloading

 
Abdul Kader
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering from a long time what is real advantage of Method Overloading?

 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to maintain the Naming Convention. Apart from that No Use.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shankar reddy telukutla:
In order to maintain the Naming Convention. Apart from that No Use.
A bit of a sweeping generalisation there. You overload a method so you can do the same thing with different types of parameters. Look at methods like Math#min() and PrintStream#println() to see examples of overloading.
 
Abdul Kader
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i do Agree,we can group similar actions by using the Method Overloading...

Is there any More than this.... If that is the only reason, why people discuses this very often


is there any thing more than this .....

I believe method Overloading is also discussed in OOPS right ??/
 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from Naming convention it won't give anything . Even performance also.
Why which is also like normal method call.

For Example :
add(int i, int j) {.......}

add( int i , int j, int k) {.......}


If you want to call those methods
add(3,4)

here It may give performance issue , Because it has to search method name and then search for method parameters at last has to search for type of arguments. So my opinion is It should be costly operation.

For Example :
add2Numbers(int i , int j){.......}
add3Numbers(int i , int j ,int k){.......}

If you call
add3Numbers(2,3,4) directly it will call appropriate method.

My Conclusion is .. Apart from Naming convention ,No other use.

Give me feedback.
 
Abdul Kader
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reddy i do Agree with you, let us see if we have any thing more than that.

Please share your thoughts if you feel any advantages..
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Shankar Reddy: What do you mean by "advantage from naming convention"? What advantage could that possibly provide any application or program?

Abdul Khader: I believe method overloading is about the "same form different uses" kind of thing. The example for add() with different number of arguments was a little crude, I feel. Consider a different scenario :-
1) add(int,int) and
2) add(float, float)
which IMHO would make so much more sense than
1) addInt(int, int) and
2) addFloat(float, float) right?

Yes internally reflection will have to be used to identify the correct implementation that needs to be invoked based on the arguments but I don't feel that would be a huge effect on the performance. Anyways overloading was always one of the basic advantages of OOPs. One other advantage I can think of now is it improves the readability of the code. I say Anybody can write code but writing code that can be maintained by himself or somebody else can be considered a challenge in itself.

Check out some of these links :-
http://www.geekinterview.com/question_details/17255
http://www.geekinterview.com/Interview-Questions/Concepts/OOPS

Cheers,
Raj.
[ May 13, 2008: Message edited by: Raj Kamal R ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raj Kamal R:

Yes internally reflection will have to be used to identify the correct implementation that needs to be invoked based on the arguments but I don't feel that would be a huge effect on the performance.


In fact, it doesn't have any effect on performance at all. Method signatures are fully resolved at compile time. Basically, you can think of it as the argument types being part of the method name at compile time.
 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I agree with you Raj. All we are havnig the same Opinion.
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According K&B, the advantage of overloading is that we provide multiple interface(not java interface) to make the life easier for programmer. say for example, you have one method process(String id) which take id to process the data. But your application is sending id as a Long data type.So you can have one more method process(Long id) .Now programmer will have multiple options, he doesn't need to convert first string into long to do the task.This way is good in applications which send same data as different data types.
Hope you will get the point what i am trying to say

thanks
Raj
[ May 14, 2008: Message edited by: raj malhotra ]
 
Abdul Kader
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What ever we discuessed we are coming to point of Code Readability .

I believe the Readability is the only advantage using Method Overloading...

So nothing more than that..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic