Java Polymorphism Concepts – Complete Guide with Examples


Learn the concepts of polymorphism in Java, including compile-time and runtime polymorphism, with detailed examples to implement flexible and maintainable object-oriented programs.

Polymorphism Concepts in Java – Complete Detailed Tutorial

Polymorphism is a core concept of Object-Oriented Programming (OOP) that means "many forms".

It allows objects to take multiple forms and enables flexible code and method overriding/overloading.

1. Types of Polymorphism in Java

Java supports two types of polymorphism:

  1. Compile-time Polymorphism (Static Polymorphism)
  2. Runtime Polymorphism (Dynamic Polymorphism)

2. Compile-time Polymorphism (Method Overloading)

  1. Occurs when the method to be called is determined at compile time
  2. Achieved using method overloading and operator overloading (not in Java)

Example – Compile-time Polymorphism:


class Calculator {
int add(int a, int b) {
return a + b;
}

double add(double a, double 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 calc = new Calculator();
System.out.println(calc.add(10, 20)); // int version
System.out.println(calc.add(5.5, 6.5)); // double version
System.out.println(calc.add(1, 2, 3)); // 3-int version
}
}

Output:


30
12.0
6

Explanation:

  1. The compiler determines which add method to call based on parameters

3. Runtime Polymorphism (Method Overriding)

  1. Occurs when the method to be called is determined at runtime
  2. Achieved using inheritance and method overriding
  3. Uses dynamic method dispatch

Example – Runtime Polymorphism:


class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}

public class Main {
public static void main(String[] args) {
Animal a1 = new Dog(); // parent reference to Dog
Animal a2 = new Cat(); // parent reference to Cat

a1.sound(); // Dog's method called at runtime
a2.sound(); // Cat's method called at runtime
}
}

Output:


Dog barks
Cat meows

Explanation:

  1. The object type at runtime decides which method to call

4. Polymorphism with Parameters (Polymorphic Arguments)

  1. Methods can accept parent class references, and child objects can be passed

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}

class Zoo {
void makeSound(Animal a) { // polymorphic parameter
a.sound();
}
}

public class Main {
public static void main(String[] args) {
Zoo zoo = new Zoo();
zoo.makeSound(new Dog());
zoo.makeSound(new Cat());
}
}

Output:


Dog barks
Cat meows

5. Key Points of Polymorphism

  1. Polymorphism = Many Forms
  2. Compile-time Polymorphism: Method overloading, operator overloading
  3. Runtime Polymorphism: Method overriding, dynamic method dispatch
  4. Enables flexible, reusable, and maintainable code
  5. Achieved using inheritance, interfaces, and overriding