Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone please tell me the advantage of method overloading. I know that method overloading means methods having same name but different argument lists. This same thing can be done using different function names also.And both these ways require same compilation time and other resources also.
Hence please tell me the major advantage of method overloading in programming point of view.. Thank You
Posted

Which would you rather remember: one method name, with 6 overloads, or 6 different method names:
ComputeFromDataTableLengthWidthAndHeight
ComputeFromDictionaryLengthWidthAndHeight
ComputeFromDataTableWidthAndHeight
ComputeFromDictionaryWidthAndHeight
ComputeFromDataTableAndHeight
ComputeFromDictionaryAndHeight
And which would you rather type? Or read in the code?

In the old days, we had limited everything, so you got a sequence of functions with similar names:
ComputeDTLWH
ComputeDLWH
ComputeDTWH
ComputeDWH
ComputeDTH
ComputeDH
It's too easy to make a mistake!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Feb-12 23:09pm    
My 5. I would rather say: it wasn't really easier to make a mistake, it was harder to find proper method.
Please also see my comments to the answer by Naerling.
--SA
Well, one obvious advantage I can think of is that you don't have to think for a name for every overload...
For example, MessageBox.Show in .NET has 21(!) overloads. Imagine having 21 methods that all do the same thing.
MessageBox.ShowWithTextOnly
MessageBox.ShowWithTextAndCaption
MessageBox.ShowWithTextAndCaptionsAndIcon
MessageBox.ShowWithTextAndCaptionsAndIconAndButtons
...
I think you get the point :)
Basically, what it does here is improve consistency and readability.
Besides, how are you going to call a constructor with different parameters if overloading is not supported?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Feb-12 23:07pm    
Good reply, my 6.
I only want to add:

1) Overloading has nothing to do with OOP, ever.
2) It's good to understand that "overloading" is just a bad term, because nothing is actually "loaded"; the sensible explanation of the term is merely "having methods of different signatures using the same name".
--SA
Sander Rossel 26-Feb-12 2:53am    
Thanks SA. The term is indeed misleading. Overloading even sounds bad, like you're loading to much and your computer might explode :)
Thank you for your solution...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900