Overview Of Polymorphism In C#

Introduction

 
Polymorphism means having many forms in programming. The ability of the function to process the message or data in more than one form is called function overloading. When inheritance is used to extend a generalized class to a more specialized class, it includes the behavior of the top class (generalized). It is important that a given instance of an object uses the correct behavior and the property of polymorphism allows this to happen automatically. I have written this article focusing on students and beginners.
 

Flow Chart

 
Overview Of Polymorphism In C#
 

Simple Keyword

 
Polymorphism is a message to be displayed in more than one form (or) the ability of an object to respond differently to different messages.
 

Method Overloading

 
Method overloading means Early binding or Static polymorphism. Static polymorphism is decided at compile-time only. Method overloading is a same method or function name, same return type but different input parameters.
 
Step 1
  1. Create a class Name “Common” (or) keep as it as your wish.
  2. To create a same method name and parameters.
  3. Write the following code to create a method overloading.
Coding
  1.  public  class Common  
  2. {  
  3.     public int Add(int x,int y)  
  4.     {  
  5.         return (x + y);  
  6.     }  
  7.     public int Add(int x,int y,int z)  
  8.     {  
  9.         return (x + y + z);  
  10.     }  
  11.   
  12. }  
Step 2
  1. Create a class Name “Test” (or) keep as it as your wish.
  2. To create a method, call the class named “Common” and implement the method.
  3. Write the following code
Overview Of Polymorphism In C# 
 
Overview Of Polymorphism In C#
 
Coding
  1. public class Test  
  2.     {  
  3.         public static void Main(string[] args)  
  4.         {  
  5.             Common Ocommon = new Common();  
  6.             int Total= Ocommon.Add(10, 20, 30);  
  7.             Console.WriteLine("Total =" + Total);  
  8.         }  
  9.     }  

Method overriding

 
Method overriding means late binding or dynamic polymorphism. Dynamic polymorphism is decided at Run-time only. Method overriding is a same method or function name, same input parameters, same return type but different class name.
 
Coding
  1. public class Program  
  2. {  
  3.     public int Add(int x,int y)  
  4.     {  
  5.         return (x + y);  
  6.     }  
  7.      
  8. }  
  9. public class MyClass  
  10. {  
  11.     public int Add(int x,int y)  
  12.     {  
  13.         return (x + y);  
  14.     }  
  15.   
  16. }  
  17. public class TestClass  
  18. {  
  19.     static void Main(string[] args)  
  20.     {  
  21.         Program Oprogram = new Program();  
  22.         int Total= Oprogram.Add(10, 20);  
  23.         Console.WriteLine("Total =" + Total);  
  24.         MyClass Omyclass = new MyClass();  
  25.         int Subtotal = Omyclass.Add(10, 25);  
  26.         Console.WriteLine("Total =" + Subtotal);  
  27.     }  
  28. }  

Difference between Method overloading and Method overriding

 
Method overloading
Method overriding
Same method name, same return type but different input parameters
Same method name, same input parameters, same return type but different class name
Method overloading doesn’t need inheritance
Method overriding need inheritance
Any Access modifier
Public Access modifier only
Method overloading is early binding
Method overriding is late binding
Method overloading is decided at compile-time only
Method Overriding is decided at Run-time only
 

Summary

 
In this article, you have got an overview of Polymorphism in C#.I have written this article focusing on beginners and students.


Similar Articles