Friday, March 8, 2019

Polymorphism in C#

Introduction

Polymorphism is a Greek word meaning "one name many forms". In other words, one object has many forms or has one name with multiple functionalities. "Poly" means many and "morph" means forms. Polymorphism provides the ability to class multiple implementations with the same name. It is one principle concept in Object Oriented Programming after encapsulation and inheritance.

Types of Polymorphism

There are basically the following two types of polymorphism in C#:
  • Static / Compile Time Polymorphism/ Early Binding.
  • Dynamic / Runtime Polymorphism/ late binding.
polymorphism
Static / Compile Time Polymorphism/ Early Binding.
 Method overloading is an example of Static Polymorphism. In Overloading, the method / function has the same name but different signatures. It is also known as Compile Time Polymorphism because the decision of which method is to be called is made at compile time. Overloading is the concept in which method names are the same with a different set of parameters.
Here the compiler checks the number of parameters passed and the type of parameter and make the decision of which method to call and it throw an error if no matching method is found.

In the following example the class has two methods with the same name "Add" but with different input parameters (the first method has three parameters and the second method has two parameters).
  1. public class AddData  
  2. {  
  3.     public int Add(int FirstNumber, int SecondNumber, int ThirdNumber)  
  4.     {  
  5.         return FirstNumber + SecondNumber + ThirdNumber;  
  6.     }  
  7.     public int Add(int FirstNumber , int SecondNumber )  
  8.     {  
  9.         return FirstNumber  + SecondNumber ;  
  10.     }  
  11. }  
  12. class Program  
  13. {  
  14.     static void Main(string[] args)  
  15.     {  
  16.         AddData  dataClass = new AddData  ();  
  17.         int addThreeParameters = dataClass.Add(45, 34, 67);  
  18.         int addTwoParameters = dataClass.Add(23, 34);  
  19.     }  
  20. }  
  • Dynamic / Runtime Polymorphism/ late binding.
     Here, the method name and the method signature (number of parameters and parameter type must be the same and may have a different implementation). Method overriding is an example of dynamic polymorphism.

Method overriding can be done using inheritance. With method overriding it is possible for the base class and derived class to have the same method name and same something. The compiler would not be aware of the method available for overriding the functionality, so the compiler does not throw an error at compile time. The compiler will decide which method to call at runtime and if no method is found then it throws an error.

Programming Example

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace RuntimePolymorphism
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Chocolate ch = new Chocolate();
  14. ch.flavor();
  15. Console.ReadKey();
  16. }
  17. }
  18. class IceCream
  19. {
  20. public IceCream()
  21. {
  22. Console.WriteLine("Class : Icecream");
  23. }
  24. public virtual void flavor()
  25. {
  26. Console.WriteLine("IceCream Type : Vanilla");
  27. }
  28. }
  29. class Chocolate : IceCream
  30. {
  31. public Chocolate()
  32. {
  33. Console.WriteLine("Class : Chocolate");
  34. }
  35. public override void flavor()
  36. {
  37. Console.WriteLine("IceCream Type : Chocolate");
  38. }
  39. }
  40. }
OUTPUT :-  Class : Icecream,Class : Chocolate,IceCream Type : ChocolateThank you  Arjun Walmiki

No comments:

Post a Comment