Difference Between Method Overloading and overriding C#

In this article we will learn what is the difference between  Method Overloading and overriding in C# with the help of example .
 I have written this article focusing on students and beginners. So before proceeding further please refer to my previous articles for a better understanding.
So let us start from Method Overloading

What is  Method Overloading ?
Creating a multiple methods in a class with same name but different parameters and types is called as method overloading.method overloading is the example of Compile time polymorphism which is  done at compile time.
 Method overloading can be achieved by using following things :
  • By changing the number of parameters used.
  • By changing the order of parameters.
  • By using different data types for the parameters.
public class Methodoveloading    
//two int type Parameters method 
    public int add(int a, int b)   
        return a + b;    
//three int type Parameters with same method same as above    
    public int add(int a, int b,int c)  
        return a + b+c;    
  //four float type Parameters with same method same as above two method    
    public float add(float a, float b,float c,float d) 
        return a + b+c+d;    
 In the above example ,their are three methods with same method same but they differ with number of parameters and type of parameters ,hence this types of methods is called as method overloading.
FAQ of Method Overloading
Q. Can method overloading has same numbers of parameters and Name with different return types?
Ans. No, because conflict is happen in methods while passing the parameters .
Now let us learn about the Method overriding
What is Method overriding ?
Creating the method in a derived class with same name, same parameters and same return type as in base class is called as method overriding.
Method overriding is the example of run time polymorphism,how its is the part of run time polymorphism i will explain in detail.
Some Key Points of  Method overriding
  •  Method overriding is only possible in derived class not within the same class where the method is declared.
  • Only those methods are overrides in the derived class which is declared in the base class with the help of virtual keyword or abstract keyword.
public class Account
    public virtual int balance()  
        return 10;  
public class Amount:Account
    public override int balance()  
        return 500;  

Output of the above Program is
  • 10 and 500
In the above small program their are two classes Account and Amount and both the classes contain same method Name that balance() in which Account class method  returns 10 and  Amount class returns 500 at run time because Account class  method is overridden in a class Amount.
The Method overriding is very useful when  we wants to return different output of same method in different class according to the need.
Let us consider the example I wants to provide the discount on particular product according to the Customer category that A,B,C in this scenario suppose A,B,C are the classes and Discount is the class which contains virtual CustDiscount () method ,then i simply override it on class A,B,C instead of writing three different methods for each class.
I hope you understand about the  Method overriding and Method overloading,In my next article we will see the real time examples of Method overriding and Method overloading.
I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.

Post a Comment


Protected by Copyscape Online Copyright Protection
Copyright © Compile Mode