Search code examples
c#.netoverloadingoverridingmethod-signature

Method signatures, overloading, overriding: are all these terms related?


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?

Solution

  • 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.