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:-- Operation Polymorphism
- Inclusion Polymorphism
Real Life Example 1:-
Water -->Ice --> vapor
Real Life Example 2:-
Polymer (Many student know about polymer in chemistry subject.)
Water -->Ice --> vapor
Real Life Example 2:-
Polymer (Many student know about polymer in chemistry subject.)
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:-
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:-
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.
- How to implement file handling concepts in c#
- How to implement collection concepts in c#
- How to implement Event handling concepts in c#
- How to implement Delegate concepts in c#
- How to use param keyword in c#
- How to implement out parameter concepts in c#
- How to implement structure and class concepts in c#
- How know about Interface concepts in c#
- How to implement Indexer concepts in c#
- How to implement properties concepts in c#
- How to implement Abstract method and Abstract class in c#
- How to implement Constructor and Destructor concepts in c#
Download Whole Attached File
Download
0 comments:
Post a Comment