Overloading and overriding

176,482

Solution 1

Overloading

Overloading is when you have multiple methods in the same scope, with the same name but different signatures.

//Overloading
public class test
{
    public void getStuff(int id)
    {}
    public void getStuff(string name)
    {}
}

Overriding

Overriding is a principle that allows you to change the functionality of a method in a child class.

//Overriding
public class test
{
        public virtual void getStuff(int id)
        {
            //Get stuff default location
        }
}

public class test2 : test
{
        public override void getStuff(int id)
        {
            //base.getStuff(id);
            //or - Get stuff new location
        }
}

Solution 2

Simple definitions for overloading and overriding

Overloading (Compile Time Polymorphism):: Functions with same name and different parameters

public class A
{
    public void print(int x, int y)
    {
        Console.WriteLine("Parent Method");
    }
}

public class B : A
{
    public void child()
    {
        Console.WriteLine("Child Method");
    }

    public void print(float x, float y)
    {
        Console.WriteLine("Overload child method");
    }
}

Overriding (Run Time Polymorphism):: Functions in the extended class with same name and same parameters as in the base class, but with different behaviors.

public class A
{
    public virtual void print()
    {
        Console.WriteLine("Parent Method");
    }
}

public class B : A
{
    public void child()
    {
        Console.WriteLine("Child Method");
    }

    public override void print()
    {
        Console.WriteLine("Overriding child method");
    }
}

Solution 3

I want to share an example which made a lot sense to me when I was learning:

This is just an example which does not include the virtual method or the base class. Just to give a hint regarding the main idea.

Let's say there is a Vehicle washing machine and it has a function called as "Wash" and accepts Car as a type.

Gets the Car input and washes the Car.

public void Wash(Car anyCar){
       //wash the car
}

Let's overload Wash() function

Overloading:

public void Wash(Truck anyTruck){
   //wash the Truck  
}

Wash function was only washing a Car before, but now its overloaded to wash a Truck as well.

  • If the provided input object is a Car, it will execute Wash(Car anyCar)
  • If the provided input object is a Truck, then it will execute Wash(Truck anyTruck)

Let's override Wash() function

Overriding:

public override void Wash(Car anyCar){
   //check if the car has already cleaned
   if(anyCar.Clean){ 
       //wax the car
   }
   else{
       //wash the car
       //dry the car
       //wax the car
   }     
}

Wash function now has a condition to check if the Car is already clean and not need to be washed again.

  • If the Car is clean, then just wax it.

  • If not clean, then first wash the car, then dry it and then wax it

.

So the functionality has been overridden by adding a new functionality or do something totally different.

Solution 4

  • Overloading = Multiple method signatures, same method name
  • Overriding = Same method signature (declared virtual), implemented in sub classes

An astute interviewer would have followed up with:

What's the difference between overriding and shadowing?

Solution 5

As Michael said:

  • Overloading = Multiple method signatures, same method name
  • Overriding = Same method signature (declared virtual), implemented in sub classes

and

  • Shadowing = If treated as DerivedClass it used derived method, if as BaseClass it uses base method.
Share:
176,482

Related videos on Youtube

Kirill Kobelev
Author by

Kirill Kobelev

The area of my interests is Source Code Analysis and Refactoring, primarily analysis of the C/C++ code. I have toolkit for parsing C/C++ sources. It can also analyze grammar conflicts in the BNF grammars. I have web site: http://www.cdsan.com that speaks about these topics. Here is an example of the object model of a small C++ class (two data fields, ctor and two simple methods) that is used in my tool kit to analyze the code:

Updated on July 08, 2022

Comments

  • Kirill Kobelev
    Kirill Kobelev about 2 years

    What is the difference between overloading and overriding.

    • Mitch Wheat
      Mitch Wheat over 15 years
      @james: did you at least try google??
    • Jon B
      Jon B over 15 years
      I'm guessing you're not going to get this one.
    • Admin
      Admin over 15 years
      james- probably not and I feel quite silly not being able to explain a simple concept
    • Michael Meadows
      Michael Meadows over 15 years
      @james my guess is you intuited these concepts without knowing the terms. I'm going to guess you're self-taught. You should probably pick up a beginner level C# book, not to learn but to reinforce what you already know. It's mind-numbing but will help immensely in tech interviews.
    • Admin
      Admin about 13 years
      you can read more on this link : intquesans.blogspot.com/2011/05/…
  • Samuel
    Samuel over 15 years
    Never knew it was called shadowing, good to know. The only relation overloading and overriding have is overing.
  • Freakyuser
    Freakyuser over 11 years
    Perhaps you would like to add that the overriding concept applies for the parent class-sub class relationship.
  • Ferrakkem Bhuiyan
    Ferrakkem Bhuiyan about 10 years
    i love this answer @cgreeno ..Thanks
  • Mahdi
    Mahdi over 9 years
    @cgreeno i think you miss void for getStuff in test2
  • Alex Stephens
    Alex Stephens about 9 years
    just to mention that the base class method must be marked as virtual to override else you'll get a compilation error.
  • user3885927
    user3885927 about 8 years
    Good one, with the needed details.
  • Sj03rs
    Sj03rs almost 8 years
    Hey, this might be old but I still have a question about this. Does overloading make it available to have more functionality with 'one' method? Like I can do this: getStuff(2, "Hello world!"); or I can do this getStuff("Myname", "Mysurname", "Hello World!");? Anyone can confirm this is the way to do it?
  • SparK
    SparK almost 8 years
    Captain here: Overloading a method gives you multiple signatures of different methods with the same name. Overriding hides an inherited member, either from classes or interfaces. flies away
  • Servy
    Servy almost 8 years
    @SparK Overriding doesn't hide a member. You can either override or hide an inherited member. The two are mutually exclusive options.
  • SparK
    SparK almost 8 years
    @Servy, wrong choice of words, I meant "substitutes".
  • Jack
    Jack over 7 years
    This must be the answer. Voted+
  • Cristina Carrasco
    Cristina Carrasco over 4 years
    I think the Overriding example actually is Overwriting, override is when you super class method get override accidentaly or if you mean to do it by using the new keyword in both cases, override or overwrite, the behaivor is diferent when you declare Parent p = new Child(); p.Method(); ... maybe I'm wrong and it is just another way to do almost the same thing. Can anyone explaint it better than me?