Polymorphism in OOP using Java

Kavindu Sandaruwan
4 min readJan 9, 2024

--

Polymorphism is one of the key concepts of OOP that allows objects of different classes to be treated as objects of a common class. It enables objects to behave differently based on their specific class type. In this blog post, we will explore what polymorphism is, how it works, and how to implement it in Java using examples.

What is polymorphism?

The word polymorphism means "many forms". In OOP, polymorphism refers to the ability of an object (or a reference to an object) to take different forms of objects. It allows a common message to be sent to each class, and each class can respond to it in its own way. For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animal could be Dog, Cat, Bird, etc. and they also have their own implementation of animalSound() (the dog barks, the cat meows, the bird chirps, etc.). Polymorphism allows us to call animalSound() on any object of type Animal, and the appropriate subclass method will be executed depending on the actual object type.

Types of polymorphism

In Java, polymorphism can be achieved in two ways: compile-time polymorphism and run-time polymorphism.

Compile-time polymorphism

Compile-time polymorphism, also known as static polymorphism, is when the compiler decides which method to call based on the number and types of arguments passed to the method. This type of polymorphism is achieved by method overloading. Method overloading is when multiple methods have the same name but different parameters. For example, we can have a class called Calculator that has two methods called add(), one that takes two integers and one that takes two doubles:

Now, we can create an object of Calculator and call the add() method with different arguments. The compiler will choose the appropriate method based on the argument types:

Output:
30
31.0

Run-time polymorphism

Run-time polymorphism, also known as dynamic polymorphism, is when the compiler decides which method to call based on the actual object type at run time. This type of polymorphism is achieved by method overriding. Method overriding is when a subclass provides a different implementation of a method that is already defined in the superclass. For example, we can have a class called Shape that has a method called draw(), and subclasses of Shape such as Circle, Square, Triangle, etc. that override the draw() method:

Now, we can create an array of Shape objects and assign different subclass objects to it. Then, we can loop through the array and call the draw() method on each element. The compiler will invoke the appropriate subclass method based on the actual object type at run time:

Output:
Drawing a shape
Drawing a circle
Drawing a square
Drawing a triangle

Benefits of polymorphism

Polymorphism has several benefits for OOP, such as:

- It allows us to write more generic and flexible code that can work with different types of objects.
- It reduces code duplication and complexity by reusing the same method for different purposes.
- It supports the principle of loose coupling, which means that the classes are less dependent on each other and can be modified or extended easily.
- It enhances the readability and maintainability of the code by providing a clear and consistent interface for communication between classes.

Conclusion

In this blog post, we have learned what polymorphism is, how it works, and how to implement it in Java using examples. We have also seen the benefits of polymorphism for OOP. Polymorphism is a powerful concept that allows us to create more dynamic and adaptable code that can handle various situations and scenarios. We hope that this blog post has helped you understand and appreciate the concept of polymorphism in OOP using Java. Thank you for reading!

--

--