Difference between Method overloading and method overriding with example

 

Method Overloading vs Method Overriding in C#



Method Overloading — Definition:

Method Overloading is a feature in C# where multiple methods can have the same name within the same class but with different parameter lists (different type, number, or order of parameters).

It is also known as:

  • Compile Time Polymorphism

  • Static Polymorphism

  • Early Binding


Method Overriding — Definition:

Method Overriding is a feature in C# where a method in a derived (child) class provides a specific implementation of a method that is already defined in its base (parent) class.

It requires the use of:

  • virtual keyword in the base class method

  • override keyword in the derived class method

It is also known as:

  • Runtime Polymorphism

  • Dynamic Polymorphism

  • Late Binding

 

FeatureMethod OverloadingMethod Overriding
ConceptSame method name, but different parameters (compile-time polymorphism)Same method name, same parameters, but different implementation in derived class (runtime polymorphism)
Class RequirementSame class or base & derived classMust have inheritance (base & derived class)
Keywords UsedNo keyword neededvirtual in the base class, override in derived class
ParametersMust be different (number, type, or order)It must be exactly the same parameters/signature
Polymorphism TypeCompile Time PolymorphismRuntime Polymorphism
Achieved ByFunction Signature ChangeInheritance & overriding base method

Live Example — Covering all scenarios

using System;
public class Program { public static void Main(string[] args) { // Method Overloading Example Calculator calculator = new Calculator(); Console.WriteLine("Method Overloading Example:"); Console.WriteLine($"Sum of 10 and 20: {calculator.Add(10, 20)}"); Console.WriteLine($"Sum of 10, 20 and 30: {calculator.Add(10, 20, 30)}"); Console.WriteLine(); // Method Overriding Example Animal genericAnimal = new Animal(); genericAnimal.Speak(); // Output: Animal makes a sound Animal dog = new Dog(); // Polymorphism - Upcasting dog.Speak(); // Output: Dog Barks Animal cat = new Cat(); cat.Speak(); // Output: Cat Meows } } // Method Overloading Example public class Calculator { // Add with 2 parameters public int Add(int a, int b) { return a + b; } // Add with 3 parameters (Overloaded Method) public int Add(int a, int b, int c) { return a + b + c; } } // Method Overriding Example public class Animal { // Base method public virtual void Speak() { Console.WriteLine("Animal makes a sound"); } } public class Dog : Animal { // Overriding Speak() method public override void Speak() { Console.WriteLine("Dog Barks"); } } public class Cat : Animal { // Overriding Speak() method public override void Speak() { Console.WriteLine("Cat Meows"); } }

Output:

Method Overloading Example: Sum of 10 and 20: 30 Sum of 10, 20 and 30: 60 Animal makes a sound Dog Barks Cat Meows

Summary — Key Differences:

Method OverloadingMethod Overriding
Same method name but different parametersSame method name and same parameters
Compile Time PolymorphismRuntime Polymorphism
No inheritance requiredInheritance is mandatory
Achieved within the same class or derived classAchieved through virtual & override keywords
Method signature should differMethod signature must remain the same

Pro Tip (Interview Point):

  • Overloading is used to ensure functionality reusability within the same class.

  • Overriding is used for the customized behavior of the base class method in the child class.

Comments

Popular posts from this blog

Refresh an Angular Component without reloading the same Component

.NET Core vs .NET Framework: How to Pick a .NET Runtime for an Application

multi color initial(Avatars) if img not available in Angular it's work angular 2,4+ all versions