Polymorphism is one of the four fundamental pillars of Object-Oriented Programming (OOP) in Java. The word Polymorphism means “many forms.” In Java, polymorphism allows a single method, object, or interface to perform different tasks depending on the situation.
Polymorphism makes Java programs:
- Flexible
- Reusable
- Scalable
- Easy to maintain
Java supports two types of polymorphism:
- Compile-Time Polymorphism (Method Overloading)
- Runtime Polymorphism (Method Overriding)
For example:
A payment system may support different payment methods:
- Credit Card
- UPI
- Net Banking
- Debit Card
Although the method name may be pay(), the behavior changes based on the payment type. This is a real-world example of polymorphism.
Polymorphism is widely used in:
- Banking Software
- Hospital Management Systems
- E-commerce Applications
- Android Development
- Enterprise Applications
- Spring Boot Projects
- Game Development
- Desktop Applications
Before learning polymorphism, you should already understand:
- Classes
- Objects
- Inheritance
- Constructors
- Method Overriding
In this chapter, you’ll solve practical Java polymorphism programs that are frequently asked in coding interviews, university exams, and Java developer assessments. Java Polymorphism practice questions with solutions help to understand the concepts.
1. Java Program to Demonstrate Method Overloading (Compile-Time Polymorphism)
Problem Statement
Write a Java program to demonstrate Method Overloading, where multiple methods have the same name but different parameter lists.
Java Solution
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
System.out.println("Sum = " + calculator.add(10, 20));
System.out.println("Sum = " + calculator.add(10, 20, 30));
}
}
Sample Output
Sum = 30
Sum = 60
Explanation
The Calculator class contains two methods with the same name add().
The compiler selects the appropriate method based on the number of arguments passed.
This is called Compile-Time Polymorphism.
Concepts Covered
- Method Overloading
- Compile-Time Polymorphism
- Methods
- Parameters
2. Java Program to Demonstrate Runtime Polymorphism
Problem Statement
Write a Java program to demonstrate Runtime Polymorphism using method overriding.
Java Solution
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();
}
}
Sample Output
Dog barks
Explanation
The reference variable belongs to the parent class, but it points to a child class object.
At runtime, Java executes the overridden method of the actual object.
This is known as Runtime Polymorphism.
Concepts Covered
- Runtime Polymorphism
- Method Overriding
- Parent Reference
- Child Object
3. Java Program to Demonstrate Method Overloading with Different Data Types
Problem Statement
Write a Java program to overload methods using different parameter data types.
Java Solution
class Display {
void show(int number) {
System.out.println("Integer : " + number);
}
void show(double number) {
System.out.println("Double : " + number);
}
}
public class Main {
public static void main(String[] args) {
Display display = new Display();
display.show(25);
display.show(12.75);
}
}
Sample Output
Integer : 25
Double : 12.75
Explanation
Both methods have the same name but accept different data types.
The compiler selects the correct method automatically based on the argument type.
Concepts Covered
- Method Overloading
- Compile-Time Polymorphism
- Different Data Types
4. Java Program to Overload Methods with Different Parameter Order
Problem Statement
Write a Java program to demonstrate Method Overloading by changing the order of parameters.
Java Solution
class Display {
void show(int number, String text) {
System.out.println("Number : " + number);
System.out.println("Text : " + text);
}
void show(String text, int number) {
System.out.println("Text : " + text);
System.out.println("Number : " + number);
}
}
public class Main {
public static void main(String[] args) {
Display display = new Display();
display.show(100, "Java");
System.out.println();
display.show("Programming", 200);
}
}
Sample Output
Number : 100
Text : Java
Text : Programming
Number : 200
Explanation
Both methods have:
- Same method name
- Same number of parameters
However, the order of parameters is different, making them valid overloaded methods.
Java distinguishes overloaded methods based on:
- Number of parameters
- Type of parameters
- Order of parameters
Concepts Covered
- Method Overloading
- Parameter Order
- Compile-Time Polymorphism
- Method Resolution
5. Java Program to Demonstrate Method Overriding Using Vehicles
Problem Statement
Write a Java program to demonstrate Method Overriding using a Vehicle parent class and a Car child class.
Java Solution
class Vehicle {
void start() {
System.out.println("Vehicle Starts");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car Starts with Push Button");
}
}
public class Main {
public static void main(String[] args) {
Vehicle vehicle = new Car();
vehicle.start();
}
}
Sample Output
Car Starts with Push Button
Explanation
The parent class provides a general implementation of the start() method.
The child class overrides it with its own version.
At runtime, Java executes the child class implementation because the object belongs to the Car class.
This is an example of Runtime Polymorphism.
Concepts Covered
- Method Overriding
- Runtime Polymorphism
- Parent Reference
- Child Object
@OverrideAnnotation
6. Java Program to Demonstrate Runtime Polymorphism with Animals
Problem Statement
Write a Java program to demonstrate Runtime Polymorphism using different animal classes.
Java Solution
class Animal {
void sound() {
System.out.println("Animals make sounds.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog says: Bark Bark");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat says: Meow Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal animal;
animal = new Dog();
animal.sound();
animal = new Cat();
animal.sound();
}
}
Sample Output
Dog says: Bark Bark
Cat says: Meow Meow
Explanation
The same parent reference (Animal) points to different child objects.
Java determines the correct method at runtime.
This demonstrates Runtime Polymorphism.
Concepts Covered
- Runtime Polymorphism
- Parent Reference
- Child Objects
- Method Overriding
7. Java Program to Demonstrate Dynamic Method Dispatch
Problem Statement
Write a Java program to demonstrate Dynamic Method Dispatch.
Java Solution
class Employee {
void work() {
System.out.println("Employee is Working");
}
}
class Manager extends Employee {
@Override
void work() {
System.out.println("Manager is Managing Team");
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Manager();
employee.work();
}
}
Sample Output
Manager is Managing Team
Explanation
Although the reference variable belongs to the Employee class, it stores a Manager object.
Java executes the overridden method of the actual object during runtime.
This mechanism is known as Dynamic Method Dispatch.
Concepts Covered
- Runtime Polymorphism
- Dynamic Method Dispatch
- Parent Reference
- Child Object
8. Java Program to Create a Payment System Using Polymorphism
Problem Statement
Write a Java program to create a payment system where different payment methods override the same method.
Java Solution
class Payment {
void pay() {
System.out.println("Processing Payment");
}
}
class UPI extends Payment {
@Override
void pay() {
System.out.println("Payment Completed using UPI");
}
}
class CreditCard extends Payment {
@Override
void pay() {
System.out.println("Payment Completed using Credit Card");
}
}
public class Main {
public static void main(String[] args) {
Payment payment;
payment = new UPI();
payment.pay();
payment = new CreditCard();
payment.pay();
}
}
Sample Output
Payment Completed using UPI
Payment Completed using Credit Card
Explanation
The same pay() method behaves differently depending on the object.
This is a real-world implementation of polymorphism used in online payment gateways.
Concepts Covered
- Runtime Polymorphism
- Method Overriding
- Real-World Example
- Payment Gateway
9. Java Program to Demonstrate Polymorphism in Banking
Problem Statement
Write a Java program to demonstrate polymorphism using different bank account types.
Java Solution
class BankAccount {
void interest() {
System.out.println("General Bank Interest");
}
}
class SavingsAccount extends BankAccount {
@Override
void interest() {
System.out.println("Savings Interest : 6%");
}
}
class CurrentAccount extends BankAccount {
@Override
void interest() {
System.out.println("Current Account Interest : 0%");
}
}
public class Main {
public static void main(String[] args) {
BankAccount account;
account = new SavingsAccount();
account.interest();
account = new CurrentAccount();
account.interest();
}
}
Sample Output
Savings Interest : 6%
Current Account Interest : 0%
Explanation
Each account type provides its own implementation of the interest() method.
Java executes the appropriate version depending on the object type.
Concepts Covered
- Runtime Polymorphism
- Banking Example
- Method Overriding
- Parent Reference
10. Java Program to Demonstrate Polymorphism Using Employee Classes
Problem Statement
Write a Java program to demonstrate polymorphism using employee roles.
Java Solution
class Employee {
void role() {
System.out.println("Employee Role");
}
}
class Developer extends Employee {
@Override
void role() {
System.out.println("Developer writes code");
}
}
class Tester extends Employee {
@Override
void role() {
System.out.println("Tester performs testing");
}
}
public class Main {
public static void main(String[] args) {
Employee employee;
employee = new Developer();
employee.role();
employee = new Tester();
employee.role();
}
}
Sample Output
Developer writes code
Tester performs testing
Explanation
The parent reference Employee points to different child objects.
Each object executes its own overridden role() method.
This demonstrates real-world polymorphism in employee management systems.
Concepts Covered
- Runtime Polymorphism
- Method Overriding
- Employee Management Example
- Parent Reference
6. Java Program to Demonstrate Runtime Polymorphism with Animals
Problem Statement
Write a Java program to demonstrate Runtime Polymorphism using different animal classes.
Java Solution
class Animal {
void sound() {
System.out.println("Animals make sounds.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog says: Bark Bark");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat says: Meow Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal animal;
animal = new Dog();
animal.sound();
animal = new Cat();
animal.sound();
}
}
Sample Output
Dog says: Bark Bark
Cat says: Meow Meow
Explanation
The same parent reference (Animal) points to different child objects.
Java determines the correct method at runtime.
This demonstrates Runtime Polymorphism.
Concepts Covered
- Runtime Polymorphism
- Parent Reference
- Child Objects
- Method Overriding
7. Java Program to Demonstrate Dynamic Method Dispatch
Problem Statement
Write a Java program to demonstrate Dynamic Method Dispatch.
Java Solution
class Employee {
void work() {
System.out.println("Employee is Working");
}
}
class Manager extends Employee {
@Override
void work() {
System.out.println("Manager is Managing Team");
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Manager();
employee.work();
}
}
Sample Output
Manager is Managing Team
Explanation
Although the reference variable belongs to the Employee class, it stores a Manager object.
Java executes the overridden method of the actual object during runtime.
This mechanism is known as Dynamic Method Dispatch.
Concepts Covered
- Runtime Polymorphism
- Dynamic Method Dispatch
- Parent Reference
- Child Object
8. Java Program to Create a Payment System Using Polymorphism
Problem Statement
Write a Java program to create a payment system where different payment methods override the same method.
Java Solution
class Payment {
void pay() {
System.out.println("Processing Payment");
}
}
class UPI extends Payment {
@Override
void pay() {
System.out.println("Payment Completed using UPI");
}
}
class CreditCard extends Payment {
@Override
void pay() {
System.out.println("Payment Completed using Credit Card");
}
}
public class Main {
public static void main(String[] args) {
Payment payment;
payment = new UPI();
payment.pay();
payment = new CreditCard();
payment.pay();
}
}
Sample Output
Payment Completed using UPI
Payment Completed using Credit Card
Explanation
The same pay() method behaves differently depending on the object.
This is a real-world implementation of polymorphism used in online payment gateways.
Concepts Covered
- Runtime Polymorphism
- Method Overriding
- Real-World Example
- Payment Gateway
9. Java Program to Demonstrate Polymorphism in Banking
Problem Statement
Write a Java program to demonstrate polymorphism using different bank account types.
Java Solution
class BankAccount {
void interest() {
System.out.println("General Bank Interest");
}
}
class SavingsAccount extends BankAccount {
@Override
void interest() {
System.out.println("Savings Interest : 6%");
}
}
class CurrentAccount extends BankAccount {
@Override
void interest() {
System.out.println("Current Account Interest : 0%");
}
}
public class Main {
public static void main(String[] args) {
BankAccount account;
account = new SavingsAccount();
account.interest();
account = new CurrentAccount();
account.interest();
}
}
Sample Output
Savings Interest : 6%
Current Account Interest : 0%
Explanation
Each account type provides its own implementation of the interest() method.
Java executes the appropriate version depending on the object type.
Concepts Covered
- Runtime Polymorphism
- Banking Example
- Method Overriding
- Parent Reference
10. Java Program to Demonstrate Polymorphism Using Employee Classes
Problem Statement
Write a Java program to demonstrate polymorphism using employee roles.
Java Solution
class Employee {
void role() {
System.out.println("Employee Role");
}
}
class Developer extends Employee {
@Override
void role() {
System.out.println("Developer writes code");
}
}
class Tester extends Employee {
@Override
void role() {
System.out.println("Tester performs testing");
}
}
public class Main {
public static void main(String[] args) {
Employee employee;
employee = new Developer();
employee.role();
employee = new Tester();
employee.role();
}
}
Sample Output
Developer writes code
Tester performs testing
Explanation
The parent reference Employee points to different child objects.
Each object executes its own overridden role() method.
This demonstrates real-world polymorphism in employee management systems.
Concepts Covered
- Runtime Polymorphism
- Method Overriding
- Employee Management Example
- Parent Reference
Chapter Summary
In this chapter, you learned one of the most important concepts of Object-Oriented Programming—Polymorphism. Polymorphism allows a single interface, method, or object reference to represent different forms and behaviors, making Java programs more flexible and reusable.
Throughout this chapter, you practiced:
- Method Overloading (Compile-Time Polymorphism)
- Method Overriding (Runtime Polymorphism)
- Dynamic Method Dispatch
- Parent Class References
- Child Class Objects
- Constructor Overloading
- Real-world examples using:
- Payment Systems
- Banking Applications
- Employee Management
- Vehicle Systems
- Bird Hierarchy
- Notification Systems
- Shape Drawing
You also learned how Java decides which method to execute:
- At compile time for overloaded methods.
- At runtime for overridden methods.
Polymorphism is one of the most frequently used OOP concepts in enterprise Java applications and is essential for writing scalable, maintainable, and extensible software.
Key Takeaways
- Polymorphism means one interface, many forms.
- Java supports two types of polymorphism:
- Compile-Time Polymorphism
- Runtime Polymorphism
- Method Overloading is an example of Compile-Time Polymorphism.
- Method Overriding is an example of Runtime Polymorphism.
- Dynamic Method Dispatch selects the correct overridden method during runtime.
- Parent class references can point to child class objects.
- Polymorphism improves code flexibility and reusability.
- Real-world Java applications extensively use polymorphism.
- Understanding polymorphism is essential before learning abstract classes and interfaces.
- Polymorphism is one of the most commonly asked Java interview topics.
Frequently Asked Questions (FAQs)
1. What is polymorphism in Java?
Polymorphism is an Object-Oriented Programming concept that allows the same method or object reference to perform different actions depending on the object involved.
2. What are the types of polymorphism in Java?
Java supports two types:
- Compile-Time Polymorphism (Method Overloading)
- Runtime Polymorphism (Method Overriding)
3. What is Compile-Time Polymorphism?
Compile-Time Polymorphism occurs when multiple methods have the same name but different parameter lists.
Example:
add(int a, int b)
add(int a, int b, int c)
The compiler decides which method to call.
4. What is Runtime Polymorphism?
Runtime Polymorphism occurs through method overriding.
The method that executes is determined during program execution based on the actual object.
Example:
Animal animal = new Dog();
animal.sound();
5. What is Dynamic Method Dispatch?
Dynamic Method Dispatch is Java’s mechanism for selecting the correct overridden method during runtime.
It enables Runtime Polymorphism.
6. What is the difference between Method Overloading and Method Overriding?
| Method Overloading | Method Overriding |
|---|---|
| Compile-Time | Runtime |
| Same Class | Parent & Child Classes |
| Different Parameters | Same Parameters |
| Improves Flexibility | Enables Runtime Polymorphism |
7. Why is polymorphism important?
Polymorphism helps developers:
- Reduce code duplication
- Improve flexibility
- Increase maintainability
- Write scalable applications
- Extend software without modifying existing code
8. Where is polymorphism used in real-world applications?
Polymorphism is widely used in:
- Banking Systems
- E-commerce Applications
- Payment Gateways
- Android Development
- Enterprise Software
- Hospital Management Systems
- Spring Boot Applications
- Game Development
- Notification Systems
- Employee Management Software
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
