3

The terms "overloading" and "overriding" sound very similar (that is why they are opposed sometimes to each other), but are these two notions technically related?

  1. Are the terms "overloading" and "overriding" related?

The term "overloading" depends on "method signature" definition. So I have got a similar question.

  1. (related one) Is the term "method signature" related to "overriding" as well?

2 Answers 2

8

Overloading is having several functions with the same name, but different parameters. For example

For example

void SayHi(string name) { ... }
void SayHi(string, int age) {.... }

these are overloads.

An override "replaces" an existing function, so you're taking an existing function and providing an entirely new one with the same name and same parameters

class Person
{
    public virtual void SayHi(string name)
    {
        // ....
    }
}

class Teenager : Person
{
    public override void SayHi(string name)
    {
        // ....
    }
}

The method signature is related to overriding in the way that the new, overriding function must have the same method signature as the method that it tries to override, and also the same return type.

The method signature is related to overloading in such a way that overloads must have different method signatures.

3
  • Exactly: overriding is related to "method signature" and "the return type", but overloading is related only to "method signature". So there are two differences between these notions. The first difference between overriding and overloading is their purpose. And the second is that the return type is checked for overriding, but is ignored for overloading (to avoid ambiguity of the methods like: public int GiveMeSomething() and public string GiveMeSomething() in the same type). Feb 21, 2016 at 21:09
  • I expected to find the second difference on MSDN "method signature" page since all these notions ("method signature", "overriding" and ""overloading") are related. Feb 21, 2016 at 21:17
  • Caveat: normally the return type is ignored for purposes of the method signature. If you override a method in a statically-typed language, you either need to keep the return type the same or be careful of the new return type and any virtual declarations to make sure what's returned at run-time matches what the compiler will expect. This of course isn't an issue in dynamically-typed languages.
    – Todd Knarr
    Feb 22, 2016 at 2:59
3

Overloading refers to having multiple versions of the same method or function name where each one has different argument types, eg. init( int, int ) and init( const char * ) in C++, with implementations specific to the argument types. The compiler will select which version to call based on the arguments you use in a particular call. The method signature refers to the types and order of the arguments. For overloading, the method signatures of the different versions of the method must differ.

Overriding refers to a derived class implementing it's own version of a method declared in a base class, replacing the base-class implementation of the method with one specific to the derived class. For overriding, the method signature of the derived-class method must be identical to that of the base-class method. If it differs, the derived class is overloading the base-class method, not overriding it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.