Method Overloading and Method overriding C#

Background

In this article we will learn about one of the most reusable object oriented features of C#,Method Overloading and Method  overriding . We will learn about Method Overloading and Method  overriding from the basics because I have written this article focusing on students and beginners. 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.

e.g

  1. public class Methodoveloading    
  2.   {    
  3.     public int add(int a, int b)  //two int type Parameters method  
  4.     {    
  5.         return a + b;    
  6.         
  7.     }    
  8.     public int add(int a, int b,int c)  //three int type Parameters with same method same as above  
  9.     {    
  10.         return a + b+c;    
  11.     
  12.     }    
  13.     public float add(float a, float b,float c,float d)  //four float type Parameters with same method same as above two method 
  14.     {    
  15.         return a + b+c+d;    
  16.     
  17.     }    
  18.   }   
 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

Question-    Can method overloading has same numbers of parameters and Name with different return types?
Answer-  No, because conflict is happen in methods while ing 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.
 e.g
  1. public class Account
  2.   {  
  3.     public virtual int balance()  
  4.     {  
  5.         return 10;  
  6.     }  
  7.   }  
  8. public class Amount:Account
  9. {  
  10.      
  11.     public override int balance()  
  12.     {  
  13.         return 500;  
  14.     }  

 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.
 
Summary

I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.  
 

Up Next
    Ebook Download
    View all
    Learn
    View all