How to implement Polymorphism Concepts in C#

By
Polymorphism is a ability to take more than one form. An operation may exhibit different behaviour in different situations.There are two type of polymorphism in oops as given below:-
  1. Operation Polymorphism
  2. Inclusion Polymorphism 
type



Real Life Example 1:-
Water -->Ice --> vapor
Real Life Example 2:-
Polymer (Many student know about polymer in chemistry subject.)
1.) Operation Polymorphism:-
It is implemented using overloaded methods and operators. Overloading process is called early binding ,or static linking,Static binding.It is also known as compile time polymorphism. In operation polymorphism I have implemented overloading concepts with an example as given below:-
Step 1:- First open your visual studio --> File --> New -->Projects -->Select Console application -->OK--> Write the following c# program as shown below:-

using System;

namespace operation_poly
{
    class addition
    {
        public void add(int a, int b)
        {
            int r = a + b;
            Console.WriteLine("Addition of two Number = " + r);
        }
        public void add(string p, string q)
        {
            Console.WriteLine("Concatenation of two string = " + p +q);
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            //add two integer values
            addition ad = new addition();
            Console.WriteLine("Enter Two Integer values");
            int m =int.Parse(Console.ReadLine());
            int n = int.Parse(Console.ReadLine());
            ad.add(m, n); //call add method which hold integer parameter
            //add two string values
            Console.WriteLine("Enter Two String values");
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();
            ad.add(s1, s2); //call add method which hold string parameter
            Console.ReadLine();
        }
    }
}


Step 2:- Now Run the program (Press F5)-->you will see following output:-


output

Description:-
Here i have call two Add() method
ad.add(m, n);
ad.add(s1,s2);
Here you can see,One method (Add()) work as a two form,So it is called a polymorphism.
Note:- In operation polymorphism,the multiple forms occur at the method level ,rather than at class level.The same name takes multiple implementation forms.

2.) Inclusion Polymorphism :-
Inclusion polymorphism also known as Run time polymorphism. This is achieved through the use of virtual keyword with method. There are some steps to implement this concepts using c# programming as given below:-
Step 1:-First open your visual studio --> File --> New -->Projects -->Select Console application -->OK--> Write the following c# program as shown below:-

using System;

namespace Inclusion_poly
{
    class parents
    {
        public virtual void display()
        {
            Console.WriteLine("I am parents class display method");
        }
    }
    class son : parents
    {
        public override void display()
        {
            Console.WriteLine("I am child (son) class display method");
        }
    }
    class Daughter : parents
    {
        public override void display()
        {
            Console.WriteLine("I am child (daughter) class display method");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            parents pa = new parents();
            pa.display(); // call parents class display method
            pa = new son();  //upcasting
            pa.display(); // call son class display method
            pa = new Daughter(); //upcasting
            pa.display();   // call daughter class display method
            Console.ReadLine();
        }
    }
}

Step 2:-Now Run the Application (press F5)-->You will see following output as shown below:-
output

Description:- in Above, i have implemented the concepts of inclusion polymorphism.
  • You can see,I have used casting operation to implement inclusion polymorphism.
  • I have created the object of parents class 'pa'.When we cast the object of son class to 'pa' then it behaves like a son type object. Similarly when we cast the object of Daughter class to 'pa' then it behaves like a Daughter type object. H
  • In above output,you can see two identical calls produce two different outputs.So it is called Inclusion polymorphism. 
For More...
  1. How to implement file handling concepts in c#
  2. How to implement collection concepts in c#
  3. How to implement Event handling concepts in c#
  4. How to implement Delegate concepts in c#
  5. How to use param keyword in c#
  6. How to implement out parameter concepts in c#
  7. How to implement structure and class concepts in c#
  8. How know about Interface concepts in c#
  9. How to implement Indexer concepts in c#
  10. How to implement properties concepts in c#
  11. How to implement Abstract method and Abstract class in c#
  12. How to implement Constructor and Destructor concepts in c#
I hope this is helpful for you.
Download Whole Attached File
      Download

0 comments:

Post a Comment

Powered by Blogger.