Tuesday, April 14, 2020

Polymorphism in C#

What polymorphism is
Polymorphism means one thing having many (poly) forms. Suppose the example shown in the following diagram:
In the preceding example a vehicle is something that has various forms; two-wheeler, three-wheeler and four-wheeler and so on. So how to differentiate each form in the preceding example using wheels (parameters)? Let us see more about polymorphism,
There are two types of polymorphism; they are:
Compile time polymorphism
Compile time polymorphism is done at compile time. The following are examples of compile time polymorphism.  
  • Method overloading
  • Operator overloading
Method overloading
Creating multiple methods in a class with the same name but different parameters and types is called method overloading.
 Method overloading can be done in any of the following ways:
  • By changing the number of parameters used.
  • By changing the order of parameters.
  • By using different data types for the parameters. 

For example:
  1. namespace BAL  
  2. {  
  3.     public class Methodoveloading  
  4.     {  
  5.         public int add(int a, int b) //Method 1   
  6.         {  
  7.             return a + b;  
  8.   
  9.         }  
  10.         public int add(int a, int b, int c) //Method 2  
  11.         {  
  12.             return a + b + c;  
  13.   
  14.         }  
  15.         public float add(float a, float b, float c, float d) //Method 3  
  16.         {  
  17.             return a + b + c + d;  
  18.   
  19.         }  
  20.     }  
 In the preceding method their are three methods with the same name but different parameter type. This is called method overloading. In my next article we will see the program method of overloading in details.
Run time polymorphism
Run time polymorphism happens at run time, in other words values are ed at runtime to the methodRuntime polymorphism can be done using method overriding.
Method overriding
Creating the method in a derived class with the same name, same parameters and same return type as in the base class is called method overriding.
  • Method overriding is only possible in a derived class, not within the same class where the method is declared.
  • Only those methods overriden in the derived class declared in the base class using the virtual keyword or abstract keyword.
For example:
  1. namespace BAL  
  2. {  
  3.     public class methodoverriding  
  4.     {  
  5.         public virtual int balance()  
  6.         {  
  7.             return 10;  
  8.         }  
  9.     }  
  10.     public class Amount : methodoverriding  
  11.     {  
  12.   
  13.         public override int balance()  
  14.         {  
  15.             return 500;  
  16.         }  
  17.     }  
 Output
10 and 500
In the preceding program we declare the Virtual method that returns 10 and the same method we are using in the class Amount using the override keyword  that at runtime returns 500 without changing the values of the method, even the names are the same so the example above shows runtime polymorphismIn my next article we will see in details a program with runtime polymorphism.
Difference between method overloading and method overriding
Method overloading
Creating multiple methods in a class with the same name but different parameters and types is called method overloading.
Method overriding
Creating the method in a derived class with the same name, the same parameters and the same return type as in a base class is called method overriding.  
Difference between virtual method and abstract method

Some of the FAQs on polymorphism are:
Question: Can method overloading have the same number of parameters with different return types?
Ans: No, because a conflict occurs in methods when ing the parameters.
Question:  What is operator overloading?
Ans:  We can redefine operators like +- and * with additional functionalities.

No comments:

Post a Comment