Polymorphism in java can be understood as an ability of an object to take many forms. In a simple language, With the help of Polymorphism in Java we have liberty to perform multiple actions in a various ways and any java object that can process more than one IS-A test is known as Polymorphic.
Let’s take an example to understand polymorphism in a simple manner, consider a superclass called Furntiure, which has a method named FurnitureItems (). Chair, Bed, Table, and Dinning table are subclasses of Furniture and each has its own uses (the chair for sitting, the bed for sleeping, and so on):
Polymorphism”: Real life Example
A person can have a variety of relationships with different people. A Man can be a father, a son, a brother, and a friend all at once, i.e. he can act in different ways in different situations.
Same as human organs. All organs are related to human body but all have different functions. The heart is in charge of blood flow, the lungs of breathing, the brain of cognition, and the kidneys of excretion. Hence, we have a standard method function that behaves differently depending on the body organ.
Polymorphism in Java
When there is inheritance, or when there are many classes that are related to each other, polymorphism occurs. Object-Based Languages, not Object-Oriented Languages, are those that do not support polymorphism. Java is an Object-Oriented Language because it supports polymorphism. When there is inheritance, or when there are many classes that are related to each other, polymorphism occurs.
Polymorphism in Java Example
A superclass named “Furniture” has a method “Items()”. Subclasses of “furniture” can be “Sleeping, “sitting”, “Eating”, etc. Each subclass has its way of uses. Using Inheritance and Polymorphism means, the subclasses can have “uses()” method to find the uses’s formula for that furniture.
class Furniture {
public void furnitureItems() {
System.out.println(“Use of Furntiure”);
}
}
classChair
extends
Furniture
{
public
void
furnitureItems()
{
System.
out
.println("The Chair: For Sitting");
}
}
classDinning table
extends
Furniture
{
public
void
FurnitureItems()
{
System.
out
.println("The Dinning table: for eating");
}
}
Now we can create Chair and Bed objects and call the furnitureItems() method on both of them.
Example
classFurniture
{
public
void
FurnitureItems()
{
System.
out
.println("Uses of Furniture");
}
}
classChair
extends
Furniture
{
public
void
FurnitureItems()
{
System.
out
.println("The chair: For Sitting");
}
}
classDinning Table extends
Furniture{
public
void
FurnitureItems()
{
System.
out
.println("The Dinning Table: For Eating");
}
}
classMain
{
public
static
void
main(String[]
args
){
Furniture
my
Furniture=
new
Furniture();
// Create a Furniture object
Furniture
myChair
=new
Chair();
// Create a Chair object
Furniture
myDinningTable
=new
DinningTable();
// Create a DinningTable object
myFurniture
. FurniturItems();
myChair
. FurnitureItems();
myDinningTable
. FurnitureItems();
}
}
Types of Polymorphism
Polymorphism can be done in Java using two alternative methods:
- Method Overloading
- Method Overriding
Method Of Overloading
The process of creating multiple methods with the same name in the same class, each of which performs a different function, is known as method overloading.
Method Of Overriding
When a subclass or a child class has the same method as the parent class, this is known as method overriding.
Comments are closed.