What is Polymorphism?
- by SAMIR BHOGAYTA
* 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
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
}
}