From an interviewer's point of view, method overloading and method overriding and the difference between them is an important concept.
Thus, let's understand it.
What is method overloading?
Creating more than one method or a function that has a same name but different signatures or parameters in the same class is called method overloading.
Key points
- Method overloading is also called early binding or compile time polymorphism or static binding.
- The compiler automatically calls the required method or the function by checking number of parameters and their type, which are passed into that method.
- If the number of parameters and type doesn't match by any method signatures, then it will give the compile time error.
- We can achive method overloading by changing the number of parameters used or by using different types of parameters or by changing the order of parameters
Example
using System;
using System.Collections.Generic;
using System.Text;
namespace MethodOverloadingDemo
{
class Calculator
{
// Two int type parameters method
public int Add(int x, int y)
{
return x + y;
}
// Three int type parameters method
public int Add(int x, int y, int z)
{
return x + y + z;
}
// Two float type parameters method
public float Add(float x, float y)
{
return x + y;
}
// Three float type parameters method
public float Add(float x, float y, float z)
{
return x + y + z;
}
}
class CalculatorDemo
{
public static void Main(string[] args)
{
Calculator calculator = new Calculator();
// Calling the overloaded methods
int result1 = calculator.Add(10, 20);
int result2 = calculator.Add(10, 20, 30);
float result3 = calculator.Add(10.5f, 20.3f);
float result4 = calculator.Add(10.5f, 20.3f, 30.7f);
// Displaying the results
Console.WriteLine("Result of adding two integers: " + result1);
Console.WriteLine("Result of adding three integers: " + result2);
Console.WriteLine("Result of adding two floats: " + result3);
Console.WriteLine("Result of adding three floats: " + result4);
Console.ReadLine();
}
}
}
Output
What is method overriding?
Creating a method in the derived class with same signature as a method in the base class is called method overriding
or
Method overriding means having two methods with the same name and same signature, one method in the base class and the other method in the derived class.
Key points
- Method overriding is also called run time polymorphism or dynamic polymorphism or late binding.
- We can override a method in the base class by creating similar function in the derived class. This can be achieved by using inheritance and using virtual & override.
- Same signature means the methods must have the same name, same number of arguments and same type of arguments.
- Method overriding is possible only in the derived classes, but not within the same class.
- When the derived class needs a method with the same signature as in the base class, but wants to execute different code than the one provided by the base class then method overriding will be used.
- Method overriding in C# is a feature like the virtual function in C++.
Important keywords in method overriding
- Virtual keyword
If we use Virtual keyword, then we tell to compiler that this method can be overridden by the derived classes.
public virtual int Print()
{
//Implementation of Print method in Base class
}
- Override keyword
If we use Overrride keyword, then we tell to the compiler that this method is overriding the same named method in the base class.
public override int Print()
{
//Implementation of Print method in Derived class
}
- Base keyword
If we use base keyword, then we tell to the compiler that this method calls the base class method for overriding functionality.
base.Print();
Difference between method overloading and method overriding
Method Overloading |
Method Overriding |
Creating more than one method or function having same name but different signatures or the parameters in the same class is called method overloading. |
Creating a method in the derived class with the same signature as a method in the base class is called as method overriding |
It is called the compile time polymorphism |
It is called runtime polymorphism |
It has the same method name but with different signatures or the parameters |
It must have same method name as well as the signatures or the parameters. |
Method overloading doesn’t need inheritance |
Mehod overriding needs inheritance |
Method overloading is possible in single class only |
Method overriding needs hierachy level of the classes i.e. one parent class and other child class. |
Access modifier can be any |
Access modifier must be public. |
Method overloading is also called early binding. |
Method overriding is also called late binding. |