Click here to Skip to main content
15,884,991 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

This is Sachin, a newbie in oop, asking a very foolish question but need an explaination to avoid big confusion.

Why function overloading needed in Object oriented programming?
Posted
Updated 10-Apr-11 22:14pm
v2
Comments
Dalek Dave 11-Apr-11 4:15am    
Edited for spelling and grammar.

It is not needed — not at all. Function overloading is only for convenience, just to allow same name for actually different methods. Compiler can tell one method from another just by looking at the parameters of the call, where possible.

Formally, I answered this question fully. However…

What is used on OOP is overriding. This is a central point of OOP. You cannot explain it without understanding the main idea of OOP. See any manual of the topic — this is a bit beyond the format of CodeProject. It this is just a school test or interview question, experts usually don't answer such Questions.

—SA
 
Share this answer
 
v3
Comments
Abhinav S 11-Apr-11 3:15am    
5. Another approach to answer OP's question.
Sergey Alexandrovich Kryukov 11-Apr-11 3:16am    
Thank you, Abhinav. Well, not a really different approach.
The key here is: overloading has nothing to do with OOP.
--SA
CPallini 11-Apr-11 3:23am    
Well done, my 5. I like expecially the: "Is is not needed - not at all" sentence.
Sergey Alexandrovich Kryukov 11-Apr-11 3:56am    
Thank you very much.
--SA
[no name] 11-Apr-11 3:45am    
Nice one.
Hey dear first try google for this then asked question
Google[^]

but let me explain some logic and some interesting concept,
A Function overloading means same function with different parameter list like,
following
add(int k,int j)  //for adding two integer

add(float k,float j)//for adding two floats 


Now in java take example,
int indexOf(String str) //take one parameter
int indexOf(int ch, int startIndex) //overloaded with two parameter

now if there no function overloading then for same task there two saparates functions.
Like there goes increase for different purpose so
To reduce the increase order of function for same task a operator overloading introduced,
same with constructor overloading
 
Share this answer
 
Comments
Coder Block 11-Apr-11 3:49am    
Thanks santosh.
Dalek Dave 11-Apr-11 4:16am    
Good Answer
[no name] 11-Apr-11 4:37am    
Thanks Dalek
To make life easier for us all.

If I construct a generic email method, I will construct it with a number of overloads:
public static void Email(string body)
public static void Email(string body,
                         params MailAttachment[] attachments)
public static void Email(string to, string body)
public static void Email(string to,
                         string body,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject)
public static void Email(string to,
                         string body,
                         string subject,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         string fromDisplay,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         string fromDisplay,
                         string credentialUser,
                         string credentialPassword,
                         params MailAttachment[] attachments)
And these will all eventually call the final one, which will do the actual job. All the others just "fill in the blanks" with appropriate defaults, or prepare suitable parameters.

In the old days, I would have had to construct nine different routines, all with different names such as "EmailToBodySubjectFromaddressWithAttachments", and then try desperately to remember which had which parameters, or how to construct the relevant defaults. This way, I have to remember one name: "Email" and the system will help me fill in the blanks.

Much easier for me, and a whole lot more reliable to boot!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Apr-11 3:15am    
I know your style of answering this question. Well, a 5, but...
It should be noted that this has nothing to do with OOP.
See my Answer.
--SA
Philippe Mori 6-Feb-15 12:22pm    
In practice, you would never provide that much overload... You would provide those that often used and the generic one. By the way, how would you use the one that does not have "to".
OriginalGriff 6-Feb-15 13:58pm    
Default version to send direct to "webmaster at mydomain" etc.
Function overloading allows you to create different methods with the same name.
So basically, I can call the same method and based on my requirement (and implementation) the right method is automatically called.

For e.g. I can have two overloaded methods GetName(string firstname) and GetName(string firstname, string lastname). Based on whether I call the first GetName or the second one, I could get back only first name or first name and lastname concatenated together.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 11-Apr-11 3:12am    
We've been answering at the same time. My 5 for yours.
It must be just a big OP's confusion.
Please see my answer as well.
--SA

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