| Author |
Advantage of MethodOverloading
|
Abdul Kader
Ranch Hand
Joined: Apr 11, 2007
Posts: 115
|
|
I was wondering from a long time what is real advantage of Method Overloading?
|
 |
shankar reddy
Ranch Hand
Joined: Jun 04, 2007
Posts: 71
|
|
|
In order to maintain the Naming Convention. Apart from that No Use.
|
Java Lover<br /> <br />Shankar Reddy <br />SCJP1.4 (88%)
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
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
Joined: Apr 11, 2007
Posts: 115
|
|
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
Joined: Jun 04, 2007
Posts: 71
|
|
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
Joined: Apr 11, 2007
Posts: 115
|
|
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
Joined: Mar 02, 2005
Posts: 436
|
|
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
Joined: Jul 11, 2001
Posts: 14112
|
|
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.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
shankar reddy
Ranch Hand
Joined: Jun 04, 2007
Posts: 71
|
|
|
Yes I agree with you Raj. All we are havnig the same Opinion.
|
 |
raj malhotra
Ranch Hand
Joined: Feb 22, 2007
Posts: 285
|
|
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
Joined: Apr 11, 2007
Posts: 115
|
|
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..
|
 |
 |
|
|
subject: Advantage of MethodOverloading
|
|
|