Java Polymorphism
Hello there, fellow coder! Ever wondered how the same action can be performed in different ways? Well, that’s the magic of Polymorphism in Java. Let’s dive in and unravel this mystery together, shall we?
Introduction
Polymorphism is a fundamental concept in Java and other object-oriented programming languages. It’s like a chameleon, changing its behavior based on the situation. Sounds cool, right? Let’s explore more!
Table of Contents
Understanding Polymorphism
Polymorphism, derived from Greek words ‘poly’ meaning many and ‘morphs’ meaning forms, is the ability of an object to take on many forms. Imagine you’re at a party. You’re a dancer, a listener, a chatterbox, all at once. That’s polymorphism for you in the real world!
Here’s a class diagram that illustrates the concept of polymorphism in Java:

The diagram shows a superclass Animal
with a method sound()
. The subclass Dog
inherits from Animal
and overrides the sound()
method. This is a simple example of polymorphism in Java.
Types of Polymorphism in Java
In Java, we have two types of polymorphism:
- Static Polymorphism (Compile-time Polymorphism)
- Dynamic Polymorphism (Runtime Polymorphism)
Let’s understand each of them.
Static Polymorphism in Java
Static polymorphism, also known as compile-time polymorphism, occurs when the object decides which method to call during compile time. It’s like planning your day ahead. You know you’ll have breakfast, then work, then maybe hit the gym. Here’s a simple example:
class StaticPolyDemo {
void display(int a) {
System.out.println("Display method with one parameter: " + a);
}
void display(int a, int b) {
System.out.println("Display method with two parameters: " + a + " and " + b);
}
}
JavaIn this example, we have a class StaticPolyDemo
with two methods named display
. The first display
method takes one integer parameter, while the second display
method takes two integer parameters. This is an example of method overloading, which is a type of static polymorphism.
Dynamic Polymorphism in Java
Dynamic polymorphism, or runtime polymorphism, is when the object decides which method to call during runtime. It’s like deciding on the fly whether to go for a run or read a book based on the weather. Here’s a simple example:
class Animal {
void sound() {
System.out.println("Animals make sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dogs bark");
}
}
JavaIn this example, we have a superclass Animal
with a method sound
. We also have a subclass Dog
that extends Animal
and overrides the sound
method.
In both examples, we’re performing the same action (calling a method) in different ways, which is the essence of polymorphism in Java.
Understanding Method Overloading and Overriding
Method overloading and overriding are two techniques that Java uses to achieve polymorphism.
Overloading is when you have multiple methods with the same name but different parameters.
Overriding is when a subclass provides a specific implementation of a method that is already provided by its parent class.
Java Polymorphism Examples
Let’s look at some complete code examples to understand polymorphism better.
Example 1: Static Polymorphism Example
Code
class StaticPolyDemo {
void display(int a) {
System.out.println("Display method with one parameter: " + a);
}
void display(int a, int b) {
System.out.println("Display method with two parameters: " + a + " and " + b);
}
public static void main(String args[]) {
StaticPolyDemo spd = new StaticPolyDemo();
spd.display(1);
spd.display(1, 2);
}
}
JavaIn this example, we have two display
methods with different parameters. The correct method is called based on the number of parameters during compile time.
Output
Display method with one parameter: 1
Display method with two parameters: 1 and 2
JavaDetailed Explanation of Example 1: Static Polymorphism
In this example, we have a class StaticPolyDemo
with two methods named display
. The first display
method takes one integer parameter, while the second display
method takes two integer parameters. This is an example of method overloading, which is a type of static polymorphism.
In the main
method, we create an object spd
of the class StaticPolyDemo
. We then call the display
method twice, once with one argument and once with two arguments. The correct display
method is called based on the number of arguments we pass in. This decision is made at compile time, hence it’s called compile-time or static polymorphism.
Diagram to Explain Example 1: Static Polymorphism

The diagram shows the sequence of method calls in the main
method of the StaticPolyDemo
class. When spd.display(1)
is called, the display
method with one parameter is executed. When spd.display(1, 2)
is called, the display
method with two parameters is executed. This is an example of static polymorphism, where the correct method is determined at compile time based on the number of parameters.
Example 2: Dynamic Polymorphism Example
Code
class Animal {
void sound() {
System.out.println("Animals make sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dogs bark");
}
public static void main(String args[]) {
Animal a = new Dog(); // Animal reference but Dog object
a.sound(); // Calls the Dog class method
}
}
JavaIn this example, we have an Animal
reference but a Dog
object. The sound
method of the Dog
class is called during runtime.
Output
Dogs bark
Detailed Explanation of Example 2: Dynamic Polymorphism
In this example, we have a superclass Animal
with a method sound
. We also have a subclass Dog
that extends Animal
and overrides the sound
method.
In the main
method, we create an Animal
reference a
but assign it a Dog
object. This is known as upcasting. When we call the sound
method on a
, it calls the sound
method of the Dog
class. This decision is made at runtime, hence it’s called runtime or dynamic polymorphism.
In both examples, we’re performing the same action (calling a method) in different ways, which is the essence of polymorphism in Java.
Diagram to Explain Example 2: Dynamic Polymorphism
Here’s a sequence diagram that illustrates the concept of dynamic polymorphism in Java:

The diagram shows the sequence of method calls in the main
method of the Dog
class. When a.sound()
is called, it calls the sound
method of the Dog
class, even though a
is an Animal
reference. This is an example of dynamic polymorphism, where the correct method is determined at runtime based on the actual object type.
Java Method Overloading and Overriding Examples
Absolutely, let’s add two small examples to explain the concepts of method overloading and overriding in Java.
Java Method Overloading Example
Method overloading is a feature in Java that allows a class to have more than one method with the same name, but with different parameters.
class OverloadDemo {
void demo(int a) {
System.out.println("One parameter: " + a);
}
void demo(int a, int b) {
System.out.println("Two parameters: " + a + ", " + b);
}
public static void main(String args[]) {
OverloadDemo obj = new OverloadDemo();
obj.demo(10);
obj.demo(10, 20);
}
}
JavaOutput
One parameter: 10
Two parameters: 10, 20
In this example, we have a class OverloadDemo
with two methods named demo
. The first demo
method takes one integer parameter, while the second demo
method takes two integer parameters. This is an example of method overloading.
Java Method Overriding Example
Method overriding is a feature that allows a subclass to provide a specific implementation of a method that is already provided by its superclass.
class Animal {
void eat() {
System.out.println("Animals can eat");
}
}
class Dog extends Animal {
void eat() {
System.out.println("Dogs eat dog food");
}
public static void main(String args[]) {
Animal obj = new Dog();
obj.eat();
}
}
JavaOutput
Dogs eat dog food
In this example, we have a superclass Animal
with a method eat
. We also have a subclass Dog
that extends Animal
and overrides the eat
method. When we create a Dog
object with an Animal
reference and call the eat
method, it calls the eat
method of the Dog
class. This is an example of method overriding.
Wrapping Up
Polymorphism is a powerful concept in Java that allows us to perform a single action in different ways. It’s a key part of Java’s charm, making our code flexible, extensible, and easy to read. So, the next time you see a chameleon, remember polymorphism!
Frequently Asked Questions (FAQ)
-
What is a polymorphism in Java?
Polymorphism in Java is the ability of an object to take on many forms. It’s a key feature of object-oriented programming in Java.
-
What is an example of a polymorphism in Java?
An example of polymorphism in Java is having a superclass reference variable that refers to a subclass object. This allows us to perform a single action in different ways.
-
What is the difference between polymorphism and inheritance in Java?
Inheritance is a concept where the properties of one class can be inherited by another. Polymorphism, on the other hand, allows us to perform a single action in different ways.
-
Is polymorphism the same as overloading?
No, overloading is a technique that Java uses to achieve polymorphism. Overloading is when you have multiple methods with the same name but different parameters.
-
How does polymorphism improve code readability?
Polymorphism allows us to perform a single action in different ways. This means we can use a single method name to perform different tasks, making our code easier to read and understand.
Related Tutorials
- Understanding Inheritance in Java
- Deep Dive into Java Interfaces
- Mastering Abstract Classes in Java
- Exploring Method Overloading and Overriding in Java
- A Comprehensive Guide to OOP Concepts in Java
That’s all, folks! Keep coding, keep learning. Remember, practice makes perfect. Happy coding!