Polymorphism in Java: Static Vs Dynamic with Clear Examples
Updated: 11 February 2026, 12:30 pm IST
Java is one of the most popular programming languages currently. If you are learning this language, it is essential to know about its core concepts, like Polymorphism in Java. Polymorphism allows one piece of code to perform varying tasks in an application depending on the situation.
Read this blog to learn about the different types of Polymorphism in detail. It will help you understand the two types with real-world examples.
Get Complete Details From Expert
What is Polymorphism in Java?
How can you define Polymorphism in Java? In simple words, it is an object-oriented programming (OOP) concept that allows different behaviour of objects based on their class type. The term Polymorphism is formed from two Greek words- poly (many) and morph (forms). This means that entities in Java can take several forms.
Key Features of Polymorphism in Java
- Multiple Behaviours: It allows the same methods or objects to behave differently in different situations.
- Method Overriding: A child class can redefine its parent class’s method.
- Method Overloading: It allows defining multiple methods with different parameters but the same name.
- Runtime Decision: Java determines the right method to call during Runtime based on the actual class of the object.
Why Use Polymorphism in Java?
Below are the most significant reasons to use Polymorphism in Java.
- It allows the same method or class to be used with varying objects, encouraging code reusability.
- Objects from different classes can be treated as instances of a common superclass, improving flexibility in object interaction and method execution.
- Abstract classes enable working with general types, which simplifies object interaction.
- At runtime, the most appropriate method is selected, resulting in dynamic behaviour.
Also Read: Java Interview Questions With Real-Time Coding Examples
Types of Polymorphism
In Java, Polymorphism can be divided into two types: Static Polymorphism and Dynamic Polymorphism.
Compile Time Polymorphism in Java (Static)
Compile-time Polymorphism in Java is also known as Static Polymorphism and Method Overloading. This type is seen when multiple methods have the same name in the same class but different parameters.
Method Overloading
Method Overloading means that multiple functions have different parameters with the same name. Such functions are called overloaded. In Java, functions can be overloaded by changing the number of arguments or the argument type.
Method Overloading Java Polymorphism example using changing the number of arguments.
// Class 1
// Helper class
class Calculator {
// Method 1
// Sum of two integers
static int add(int a, int b) {
return a + b;
}
// Method 2
// Sum of three integers (different number of arguments)
static int add(int a, int b, int c) {
return a + b + c;
}
// Method 3
// Sum of four integers (different number of arguments)
static int add(int a, int b, int c, int d) {
return a + b + c + d;
}
}
// Class 2
// Main class
class TestOverloading {
public static void main(String[] args) {
// Calling overloaded methods
System.out.println(Calculator.add(10, 20)); // Calls method with 2 arguments
System.out.println(Calculator.add(5, 15, 25)); // Calls method with 3 arguments
System.out.println(Calculator.add(2, 4, 6, 8)); // Calls method with 4 arguments
}
}
Output
30
45
20
Explanation
The method add is overloaded three times with a different number of arguments.
- Add (10,20)- calls the method with two arguments.
- Add (5,15,25)- calls the method with three arguments.
- Add (2,4,6,8)- calls the method with four arguments.
Also Read: Types of Variables in Java: Your Guide to Writing Clean, Efficient Code
Runtime Polymorphism in Java (Static)
Dynamic Polymorphism or Runtime Polymorphism in Java refers to the method where a function call to the overridden method is resolved at Runtime. Method Overriding helps achieve this process.
Method Overriding
The process through which a subclass offers a specific method implementation defined in its superclass is known as method overriding. For this, the subclass must have the same name, parameters and return type as the superclass method. It allows a subclass to modify or extend an existing method’s behaviour in the parent class. This facilitates Dynamic Polymorphism, where the method executed is determined at Runtime depending on the actual type of the object.
Method Overriding Java Polymorphism example using an example with Animals making sounds.
// Java Program to Demonstrate Method Overriding
// Parent class
class Animal {
// Method in parent class
void sound() {
System.out.println("Animal makes a sound");
}
}
// Subclass Dog
class Dog extends Animal {
// Overriding the sound() method
@Override
void sound() {
System.out.println("Dog barks");
}
}
// Subclass Cat
class Cat extends Animal {
// Overriding the sound() method
@Override
void sound() {
System.out.println("Cat meows");
}
}
// Main class
class TestOverriding {
public static void main(String[] args) {
// Reference variable of parent type
Animal a;
// Dog object
a = new Dog();
a.sound();
// Cat object
a = new Cat();
a.sound();
}
}
Output
Dog barks
Cat meows
Explanation
- The sound() method is defined in the parent class Animal.
- Both subclasses Dog and Cat override this method with their own specific implementations.
- At Runtime, Java decides which method to call based on the actual object type (Dog or Cat), not the reference type (Animal).
Also Read: Semester-Wise Breakdown of Amity Online BCA Syllabus & Exam Patterns
Advantages of Polymorphism
The different types of Polymorphism offer the following advantages.
- It encourages code reuse.
- It enables dynamic method dispatch.
- Polymorphism simplifies maintenance.
- It helps write generic code that works with many types.
Disadvantages of Polymorphism
Although Polymorphism offers several benefits, some disadvantages come along as follows.
- It might make it difficult to understand the behaviour of an object.
- Polymorphic behaviour may need additional computations at Runtime.
Take the next step in your career ?
Conclusion
Polymorphism in Java is a significant and diverse concept. As one of the most used programming languages, Java is essential for you to learn when looking forward to a career in software engineering, developing, or related fields.
Amity University Online brings the best opportunity for you in this case. The institute offers an online Bachelor of Computer Applications. It allows you to gain skills and knowledge of top programming languages. So, enrol today and be future-ready.
Stay updated with our latest Webstories:- Become a Coding Pro with Amity's BCA!
Check Out Our Top Online Programs

