Polymorphism in Java: Understanding Its Types with Examples
Rachit Joshi
java coder

Polymorphism is one of the most important concepts in Object-Oriented Programming (OOP) and plays a major role in Java development. The word polymorphism means "many forms." In Java, polymorphism allows the same method or operation to behave differently depending on the parameters provided or the actual object involved.
Java primarily supports two types of polymorphism: compile-time polymorphism and runtime polymorphism. These are commonly achieved through method overloading and method overriding.
What Is Polymorphism in Java?
Polymorphism allows a single method name or reference to represent different behaviors. This makes Java programs more flexible, reusable, and easier to maintain.
For example, imagine a Shape class with a draw() method. Different subclasses such as Circle, Rectangle, and Triangle can provide their own implementations of draw(). The same method call can therefore produce different results depending on the object.
Polymorphism is especially useful when designing applications where different objects need to follow a common structure while implementing their own behavior.
Types of Polymorphism in Java
There are two major types:
Compile-Time Polymorphism
Runtime Polymorphism
Let's understand both.
1. Compile-Time Polymorphism
Compile-time polymorphism is commonly achieved through method overloading. In method overloading, multiple methods have the same name but different parameter lists.
For example:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}Here, both methods are named add(), but they accept different types of parameters.
When the program calls add(), the compiler determines which method should be used based on the arguments supplied.
Calculator c = new Calculator();
System.out.println(c.add(10, 20));
System.out.println(c.add(5.5, 4.5));The compiler selects the appropriate overloaded method during compilation. This is why it is called compile-time polymorphism.
2. Runtime Polymorphism
Runtime polymorphism is achieved primarily through method overriding. It occurs when a subclass provides its own implementation of a method defined in its parent class.
Consider this example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();
}
}The animal reference has the type Animal, but it points to a Dog object. At runtime, Java identifies the actual object and calls the overridden sound() method from the Dog class.
The concept is also known as Dynamic Method Dispatch or runtime polymorphism.
Polymorphism Through Upcasting
Upcasting is another important concept associated with runtime polymorphism. It occurs when a parent-class reference refers to an object of a child class.
Animal animal = new Dog();Here, Animal is the parent class and Dog is the child class. The parent reference can access the overridden methods available through the object, while Java selects the appropriate overridden implementation at runtime.
This approach allows developers to write more generalized and flexible code.
Real-World Example of Polymorphism
Consider a banking application. Different banks may provide different interest rates, but they can all follow a common Bank structure.
class Bank {
float getRate() {
return 0;
}
}
class SBI extends Bank {
float getRate() {
return 8.4f;
}
}
class ICICI extends Bank {
float getRate() {
return 7.3f;
}
}A Bank reference can point to different bank objects:
Bank bank;
bank = new SBI();
System.out.println(bank.getRate());
bank = new ICICI();
System.out.println(bank.getRate());The same getRate() method call produces different results based on the object assigned to the reference. This demonstrates runtime polymorphism.
Why Is Polymorphism Important?
Polymorphism offers several benefits in Java:
Flexibility: The same interface or parent reference can work with different objects.
Code Reusability: Common behavior can be defined once and reused.
Maintainability: Applications become easier to modify and extend.
Loose Coupling: Code can depend on general parent types instead of specific implementations.
Better OOP Design: It supports the development of scalable object-oriented applications.
Conclusion
Polymorphism is a fundamental feature of Java that allows one method or reference to take multiple forms. Method overloading provides compile-time polymorphism, while method overriding provides runtime polymorphism.
Understanding polymorphism also makes concepts such as inheritance, upcasting, method overriding, and dynamic method dispatch much easier to understand.
For more Java tutorials, programming examples, and explanations of OOP concepts, TPointTech provides a broad collection of Java learning resources for beginners and developers.
More articles by Rachit

C++ Variables – Declaration, Initialization, Types, Scope & Examples
Introduction Variables are one of the fundamental building blocks of C++ programming. A variable is a named storage location in memory that holds a value during program execution. Variables allow prog...

MAT Full Form: Understanding Its Different Meanings
Acronyms are often used in education, business, finance, and everyday communication. One such acronym is MAT , which can have different meanings depending on the context. The three important meanings...