How implement Polymorphism in C#

No.of Views1270
Bookmarked0 times
Downloads 
Votes0
By  gurumatrix2004   On  16 Feb 2010 03:02:51
Tag : CSharp , Miscellaneous
How implement Polymorphism in C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction


•    Polymorphism is one of the primary characteristics (concept) of object-oriented programming

•    Poly means many and morph means form. Thus, polymorphism refers to being able to use many forms of a type without regard to the details

•    Polymorphism is the characteristic of being able to assign a different meaning specifically, to allow an entity such as a variable, a function, or an object to have more than one form

•    Polymorphism is the ability to process objects differently depending on their data types

•    Polymorphism is the ability to redefine methods for derived classes.

Types of Polymorphism

•    Compile time Polymorphism
•    Run time Polymorphism

Compile time Polymorphism

•    Compile time Polymorphism also known as method overloading
•    Method overloading means having two or more methods with the same name but with different signatures

Example of Compile time polymorphism

Image Loading

Run time Polymorphism

•    Run time Polymorphism also known as method overriding
•    Method overriding means having two or more methods with the same name , same signature but with different implementation

Example of Run time Polymorphism

Image Loading


Example of Compile time Polymorphism

using System;
class Calculation
{
double result = 0;

public double Multiply(int x, int y)
{
result = x * y;
return result;
}

public double Multiply(int x, int y, int z)
{
result = x * y * z;
return result;
}
}
class Output
{
public static void Main()
{
Console.WriteLine("Press 1 to select multiply of two numbers or Press 2 to select multiply of three numbers");
int press=0;

press = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter first number for multiplication");
int number_one = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter second number for multiplication");

int number_two = Convert.ToInt16(Console.ReadLine());
int number_three = 0;

if (press == 2)
{
Console.WriteLine("Enter third number for multiplication");

number_three = Convert.ToInt16(Console.ReadLine());
}

Calculation objcal = new Calculation();
double getResult = 0;

if (press == 1)
{
getResult = objcal.Multiply(number_one, number_two);
}
else
{
getResult = objcal.Multiply(number_one, number_two, number_three);
}
Console.WriteLine("Result: " + getResult);

}
}

Example of Run time Polymorphism

using System;
using System.Collections.Generic;
class Shapes
{
public virtual void getArea()
{
Console.WriteLine("get area  formula for different shapes");
}

public virtual void getVolume()
{
Console.WriteLine("get volume formula for different shapes");
}
}

class Circle : Shapes
{
public override void getArea()
{
Console.WriteLine("Area of circle is 3.14 * radius * radius");
}
}

class Sphere :Shapes
{
public override void getArea()
{
Console.WriteLine("Area of Sphere is 4 * 3.14 * radius * radius");
}
}

class Prisms :Shapes
{
public override void getVolume()
{
Console.WriteLine("Volume of Prisms is breath * height");
}
}

class Pyramid : Shapes
{
public override void getVolume()
{
Console.WriteLine("Volume of Pyramid is 1/3 breath * height");
}
}

class OutPuts
{
public static void Main()
{
Shapes objshapes = new Prisms();

objshapes.getVolume();

}
}

 

Thank for reading.

 
Sign Up to vote for this article
 
About Author
 
gurumatrix2004
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-11 Aug 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
By:kushalDate Of Posted:11/27/2010 11:59:35 AM
mr
good article
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top