Overloading and overriding
What is the difference between 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?