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 polymorphismRun 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 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 Polymorphismusing 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. |