Polymorphism is mostly used when we are working with inheritance where we can have different classes with common features. For example, a subclass can inherit the class attributes and member functions of the superclass and in such cases, polymorphism makes it possible to perform a single action in numerous ways.
This write-up presents a detailed overview of polymorphism and in this regard, you will get to understand the following concepts:
Let’s begin!
What is Polymorphism
The name itself tells the whole story i.e. poly means many and morphism/morph means forms so polymorphism means multiple forms of anything. Let’s consider the below listed real-world examples to understand the concept of polymorphism:
Human: a man may behave as a student while studying, a customer while shopping, a family member while at home, etc.
Shapes: a shape can be a circle, triangle, eclipse, and so on.
Water: it can have many forms such as liquid(water), solid(ice), gas(steam).
So, from the above examples, we can conclude that anything that can possess multiple behaviors in different conditions is referred as polymorphism.
Polymorphism in Java
Let’s relate the above examples with Java to understand how polymorphism works in java and to do so consider the below-given example:
The Generic class will be declared as the superclass and other classes can extend the class attributes and methods of that class.
Here in this example, Shapes is a General Class that can’t be restricted with only a single shape like a circle, triangle, etc. So, it will have a generic implementation that can be inherited by the subclasses.
Furthermore, circle, triangle, and eclipse(the subclasses) can extend the properties of the superclass Shape.
Polymorphism makes it possible for the subclasses to inherit the methods from their superclass and override the implementation details.
How to Use polymorphism in Java
For the profound understanding of polymorphism in Java let’s consider the below-given example:
Example
The below-given code snippet creates three classes: one is a parent class(Bird) and other two are child classes(Crow and Duck):
public void BirdSound() {
System.out.println("A bird sings Tee Tee Tee");
}
}
class Crow extends Bird {
public void BirdSound() {
System.out.println("crow sound : caw caw caw");
}
}
class Duck extends Bird {
public void BirdSound() {
System.out.println("Duck sound : Quack");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Bird brd = new Bird();
Bird crw = new Crow();
Bird duk = new Duck();
brd.BirdSound();
crw.BirdSound();
duk.BirdSound();
}
}
The complete code with respective output is provided in the below-given screenshot:
The output verifies the working of polymorphism i.e one method with different implementations.
Conclusion
Polymorphism allows us to create multiple methods with the same name but different implementations. It comes into action when we have multiple related classes by means of inheritance. This means polymorphism allows the subclasses to inherit the methods from their superclass and override the implementation details. This write-up explains the working of polymorphism and gives a better understanding of how to use polymorphism in Java.