What Does "Overloaded"/"Overload"/"Overloading" Mean?

21,303

Solution 1

It means that you are providing a function (method or operator) with the same name, but with a different signature. For example:

void doSomething();
int doSomething(string x);
int doSomething(int a, int b, int c);

Solution 2

Basic Concept

Overloading, or "method overloading" is the name of the concept of having more than one methods with the same name but with different parameters.

For e.g. System.DateTime class in c# have more than one ToString method. The standard ToString uses the default culture of the system to convert the datetime to string:

new DateTime(2008, 11, 14).ToString(); // returns "14/11/2008" in America

while another overload of the same method allows the user to customize the format:

new DateTime(2008, 11, 14).ToString("dd MMM yyyy"); // returns "11 Nov 2008"

Sometimes parameter name may be the same but the parameter types may differ:

Convert.ToInt32(123m);

converts a decimal to int while

Convert.ToInt32("123");

converts a string to int.

Overload Resolution

For finding the best overload to call, compiler performs an operation named "overload resolution". For the first example, compiler can find the best method simply by matching the argument count. For the second example, compiler automatically calls the decimal version of replace method if you pass a decimal parameter and calls string version if you pass a string parameter. From the list of possible outputs, if compiler cannot find a suitable one to call, you will get a compiler error like "The best overload does not match the parameters...".

You can find lots of information on how different compilers perform overload resolution.

Solution 3

A function is overloaded when it has more than one signature. This means that you can call it with different argument types. For instance, you may have a function for printing a variable on screen, and you can define it for different argument types:

void print(int i);
void print(char i);
void print(UserDefinedType t);

In this case, the function print() would have three overloads.

Solution 4

It means having different versions of the same function which take different types of parameters. Such a function is "overloaded". For example, take the following function:

void Print(std::string str) {
  std::cout << str << endl;
}

You can use this function to print a string to the screen. However, this function cannot be used when you want to print an integer, you can then make a second version of the function, like this:

void Print(int i) {
  std::cout << i << endl;
}

Now the function is overloaded, and which version of the function will be called depends on the parameters you give it.

Solution 5

Others have answered what an overload is. When you are starting out it gets confused with override/overriding.

As opposed to overloading, overriding is defining a method with the same signature in the subclass (or child class), which overrides the parent classes implementation. Some language require explicit directive, such as virtual member function in C++ or override in Delphi and C#.

using System;

public class DrawingObject
{
    public virtual void Draw()
    {
        Console.WriteLine("I'm just a generic drawing object.");
    }
}

public class Line : DrawingObject
{
    public override void Draw()
    {
        Console.WriteLine("I'm a Line.");
    }
}
Share:
21,303
Steve
Author by

Steve

Updated on April 17, 2020

Comments

  • Steve
    Steve about 4 years

    What does "Overloaded"/"Overload" mean in regards to programming?

  • Yuval Adam
    Yuval Adam over 15 years
    Actually I don't like the first example. Note that changing method return type (void instead of int in this example) does not consist of overloading. It is a completely different method. Overloading refers to changing parameters types only
  • Epitaph
    Epitaph over 15 years
    However, it is not impossible to achieve as can be seen from here today.java.net/pub/a/today/2008/07/31/…
  • Ashish
    Ashish over 14 years
    i think signature includes function name also , so it should be function with the same name but different argument .
  • Camilo Martin
    Camilo Martin over 13 years
    @Yuval no, I'm afraid you're wrong. Return types are not part of the method signature according to C# spec section 1.6.6
  • Yuval Adam
    Yuval Adam over 13 years
    @Camilo - we have no argument, that's exactly what I said.
  • Camilo Martin
    Camilo Martin over 13 years
    @Yuval you said it doesn't consist of overloading, but I think it does, since you can't have both a void and a function with the same name (but then again, I'd always keep the same return type when overloading).
  • Yuval Adam
    Yuval Adam over 13 years
    @Camilo - we are both correct. It is not overloading, and it is an illegal situation. Most languages will throw a compiler error on <A> foo() and <B> foo().
  • Camilo Martin
    Camilo Martin over 13 years
    @Yuval We agree anyway, but in the answer's example it is overloading (since the parameters, not just the types, are different).
  • dainichi
    dainichi over 12 years
    @Yuval This is a language-agnostic question. In some languages, e.g. Haskell, you can overload in the return type.
  • Félix Adriyel Gagnon-Grenier
    Félix Adriyel Gagnon-Grenier about 10 years
    For us that don't know what overloading is, this answer does not really clarify. I had to read through @Serhat answer to actually catch this. probably most of the upvoters already knew what it was, and so find it clear?