POLYMORPHISM

Poly means many.
Morphism means forms.

Polymorphism is nothing but a method exhibiting different forms.

polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages.

This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

There are two types of polymorphism, they are
1.OVERLOADING 2.OVERRIDING

1.OVERLOADING:

It is known as compile time polymorphism.

Within a class, two methods with same name but variation in number of parameters or type of parameters.

2.OVERRIDING:

It is known as run time polymorphism.

Across two class inheritance. Two methods with same name and same number or type of of parameters. So, the child class will override(replace) the parent class method.

DIFFERENCE BETWEEN OVERLOADING AND OVERRIDING:

Example of overloading:

class A
{
int add(int a,int b)
{
//method body
}
int add(int a,int b,int c)
{
//method body
}
}

Example of overriding:

class A
{
int m()
{
//method body
}
int n()
{
//method body
}
}
class B extends A
{
int m()
{
//method body
}
}
B obj = new B();
obj.m();
obj.n();


Why we need polymorphism in java?

It assists the developer to reprocess the program codes. It means that the old codes and classes written once confirmed and executed can be reused as necessary, which saves a programmer’s time. Thereby these codes and related classes may be relevant to several other methods.

Only one variable can be applied for storing multiple data types such as Int, Float, Double, Long, etc. It makes it easier to search and implement these types of variables used by the users.

Offers flexibility to the programmers to write programs which uses only one method for many executions on the basis of need.

It permits the programming code to extend itself to make use of the previous program and save the developers’ time and effort.

This Polymorphism feature allows programming to become faster and resourceful to develop codes.

This is a concept of object-oriented programming language, which states a programming language capability to execute objects on the basis of their data types or class.

Therefore, generally, we can say that Polymorphism advantages give us the coding ability for the derived classes for redefining methods.

Leave a comment

Design a site like this with WordPress.com
Get started