Polymorphism With Real Life Scenario In C#

What is polymorphism-with-real-life-scenario-in-C-Sharp & can you give me real life scenario? I have faced this question during many interviews & seen many time on different forums, people are asking the same. But from very few places we are getting a perfect answer that is easy to understand. So as per my understanding & experience I would like to clear this topic through this article.
 
Polymorphism

Polymorphism word made with 2  words poly & morph  where poly means “multiple” and morph means “forms” so polymorphism-with-real-life-scenario-in-C-Sharp means many forms. Polymorphism used to provide different functionality of methods with same name and different parameter in same class & with same name and same parameter in different class.
 
Basically there are 2 kind of polymorphism-with-real-life-scenario-in-C-Sharp: 
  1. Overloading (Compile Time Polymorphism / Static Binding / Early Binding)
  2. Overriding (Runtime Time Polymorphism / Dynamic Binding / Late Binding) 
Overloading

In overloading we are keeping two or more than two methods in same class with same name but different parameters. The parameter difference may be in term of no. of parameter or in term of type of parameter or order of parameter. Overloading also called Compile Time Polymorphism / Static Binding / Early Binding because here method execution occur at compile time.
 
Note: It doesn't depend on return type of method.
 
Real life scenario

So suppose you are working on a maintenance project. And you are working on a class where already parameterised constructors has defined & you need to pass some parameter. So what you will do, either add required parameter with one constructor or add new one as per your requirement. So you should not add the required parameter with one constructor because this may disturb your other class dependency structure. So what you will do create a new constructor with required parameter. That new constructor is nothing but constructor overloading. Overriding also called Runtime Time Polymorphism / Dynamic Binding / Late Binding because here method execution happen at run time.
 
Example: 
  1. Class CEmployee  
  2. {  
  3.       pubilc  Employee(int a_EmpID, string a_PANCardNo)  
  4.       {  
  5.           // Do something  
  6.       }   
  7.         
  8.       pubilc Employee(int a_EmpID, string a_PANCardNo, string a_AadharCardNo)  
  9.       {  
  10.             // Do something   
  11.       }  
  12. }   
Overriding

In overriding we are extending the base class method in derived class. So for this first we need to make virtual base class method (use virtual keyword with base class method), then we will be able to override that base class method in derived class (use override keyword in derived class with same method of base class). 
 
Note: It depend on method return type, name & parameters.
 
Real life scenario

Suppose you need to add some custom feature to a control. Like you are using one treeview with checkbox & you would like to do something after clicking node or checkbox then you are overriding "OnAfterCheck" method of TreeView class (System.Windows.Forms.TreeView). It means when you like to extend the features of some existing class then you need to override the existing class methods.
Example
  1. public partial class CTreeViewComponent : System.Windows.Forms.TreeView  
  2. {       
  3.       protected override void OnAfterCheck(System.Windows.Forms.TreeViewEventArgs e)  
  4.       {  
  5.             // Do something   
  6.       }   
  7.    
  8.       protected override void OnAfterExpand(System.Windows.Forms.TreeViewEventArgs e)  
  9.       {  
  10.              // Do something  
  11.       }   
  12. }   
While working on some projects, you already have through these scenarios and things many times. These are very simple things but during interviews generally we stuck.


Similar Articles