Overloading and overriding
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.
Related videos on Youtube
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, 2022Comments
-
Kirill Kobelev about 2 years
What is the difference between overloading and overriding.
-
Mitch Wheat over 15 years@james: did you at least try google??
-
Jon B over 15 yearsI'm guessing you're not going to get this one.
-
Admin over 15 yearsjames- probably not and I feel quite silly not being able to explain a simple concept
-
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 about 13 yearsyou can read more on this link : intquesans.blogspot.com/2011/05/…
-
-
Samuel over 15 yearsNever knew it was called shadowing, good to know. The only relation overloading and overriding have is overing.
-
Freakyuser over 11 yearsPerhaps you would like to add that the overriding concept applies for the parent class-sub class relationship.
-
Ferrakkem Bhuiyan about 10 yearsi love this answer @cgreeno ..Thanks
-
Mahdi over 9 years@cgreeno i think you miss
void
forgetStuff
intest2
-
Alex Stephens about 9 yearsjust to mention that the base class method must be marked as virtual to override else you'll get a compilation error.
-
user3885927 about 8 yearsGood one, with the needed details.
-
Sj03rs almost 8 yearsHey, 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 thisgetStuff("Myname", "Mysurname", "Hello World!");
? Anyone can confirm this is the way to do it? -
SparK almost 8 yearsCaptain 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 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 almost 8 years@Servy, wrong choice of words, I meant "substitutes".
-
Jack over 7 yearsThis must be the answer. Voted+
-
Cristina Carrasco over 4 yearsI 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?