Difference Between Method Overloading And Method Overriding

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 overloadingthe

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

  1. Method overloading is also called  early binding or compile time polymorphism or static binding.
  2. The compiler automatically calls the required method or the function by checking number of parameters and their type, which are passed into that method.
  3. If the number of parameters and type doesn't match by any method signatures, then it will give the compile time error.
  4. 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 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.    
  5. namespace MethodOverloadingDemo  
  6. {  
  7.     class Calculator  
  8.     {  
  9.         //two int type Parameters method    
  10.         public int Add(int x, int y)  
  11.         {  
  12.             int p;  
  13.             return p=x + y;  
  14.         }  
  15.         //three int type Parameters method    
  16.         public int Add(int x, int y,int z)  
  17.         {  
  18.             int q;  
  19.             return q = x + y + z;  
  20.         }  
  21.         //two float type Parameters method    
  22.         public float Add(float x, float y)  
  23.         {  
  24.             float r;  
  25.             return r = x + y;  
  26.         }  
  27.           
  28.         //three float type Parameters method    
  29.         public float Add(float x, float y, float z)  
  30.         {  
  31.             float v;  
  32.             return v = x + y + z;  
  33.         }  
  34.     }  
  35.   
  36.    
  37. class calculatorDemo  
  38. {  
  39.     public static void Main(String[] args)  
  40.     {  
  41.         Calculator calculator = new Calculator();  
  42.         calculator.Add(10,20);  
  43.         calculator.Add(10,20,30);  
  44.         calculator.Add(10f,20f);  
  45.         calculator.Add(10f,20f,30f);  
  46.   
  47.         Console.ReadLine();  
  48.     }  
  49. }  
  50. }   

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

  1. Method overriding is also called run time polymorphism or dynamic polymorphism or late binding.
  2. 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.
  3. Same signature means the methods must have the same name, same number of arguments and same type of arguments.
  4. Method overriding is possible only in the derived classes, but not within the same class.
  5. 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.
  6. Method overriding in C# is a feature like the virtual function in C++.

Important keywords in method overriding

  1. Virtual keyword

    If we use Virtual keyword, then we tell to compiler that this method can be overridden by the derived classes.

    1. public virtual int Print()  
    2. {  
    3.    //Implementation of Print method in Base class  
    4. }  
  2. 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.

    1. public override int Print()  
    2. {  
    3.    //Implementation of Print method in Derived class  
    4. }  
  1. Base keyword

    If we use base keyword, then we tell to the compiler that this method calls the base class method for overriding functionality.

    1. base.Print();  

Comparison between method overloading and method overriding.

Sr. NoMethod OverloadingMethod Overriding
1.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
2.It is called the compile time polymorphismIt is called runtime polymorphism
3.It has the same method name but with different signatures or the parametersIt must have same method name as well as the signatures or the parameters.
4.Method overloading doesn’t need inheritanceMehod overriding needs inheritance
5.Method overloading is possible in single class onlyMethod overriding needs hierachy level of the classes i.e. one parent class and other child class.
6.Access modifier can be anyAccess modifier must be public.
7.Method overloading is also called early binding.Method overriding is also called late binding.