What is Polymorphism?
Posted
by
SAMIR BHOGAYTA
on Samir ASP.NET with C# Technology
See other posts from Samir ASP.NET with C# Technology
or by SAMIR BHOGAYTA
Published on 2010-04-10T05:27:00.000-07:00
Indexed on
2010/12/06
17:00 UTC
Read the original article
Hit count: 704
What is Polymorphism?
* 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
public class Calculations
{
public int add(int x, int y)
{
return x+y;
}
public int add(int x, int y, int z)
{
return x+y+z;
}
}
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
class Circle
{
public int radius = 0;
public double getArea()
{
return 3.14 * radius * radius
}
}
class Sphere
{
public double getArea()
{
return 4 * 3.14 * radius * radius
}
}
© Samir ASP.NET with C# Technology or respective owner