0
0
JavaConceptBeginner · 3 min read

What is Polymorphism in Java: Explained with Examples

In Java, polymorphism means an object can take many forms, allowing one interface to control access to different underlying forms (classes). It enables methods to behave differently based on the object that calls them, supporting flexible and reusable code.
⚙️

How It Works

Polymorphism in Java works like a universal remote control that can operate many devices. Instead of having a separate remote for each device, you use one remote that changes behavior depending on the device it controls. Similarly, in Java, a single method call can perform different actions depending on the object it is called on.

This happens because Java allows a parent class reference to point to child class objects. When a method is called on this reference, Java decides at runtime which child class method to execute. This is called dynamic method dispatch and is the core of polymorphism.

💻

Example

This example shows a parent class Animal and two child classes Dog and Cat. The sound() method behaves differently depending on the actual object type.

java
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 myAnimal;

        myAnimal = new Dog();
        myAnimal.sound();  // Calls Dog's sound

        myAnimal = new Cat();
        myAnimal.sound();  // Calls Cat's sound
    }
}
Output
Dog barks Cat meows
🎯

When to Use

Use polymorphism when you want to write flexible and reusable code that can work with different types of objects through a common interface. It is especially useful in situations like:

  • Implementing multiple behaviors for a method depending on object type.
  • Designing systems where new object types can be added without changing existing code.
  • Working with collections of objects that share a parent class or interface.

For example, in a drawing app, you can have a list of shapes (circles, squares, triangles) and call the same draw() method on each shape without knowing its exact type.

Key Points

  • Polymorphism allows one interface to represent different underlying forms (classes).
  • It is achieved in Java mainly through method overriding and dynamic method dispatch.
  • Supports code flexibility, extensibility, and easier maintenance.
  • Enables writing generic code that works with different object types.

Key Takeaways

Polymorphism lets one method call behave differently based on the object type.
It enables flexible and reusable code by using parent class references for child objects.
Dynamic method dispatch decides at runtime which method implementation to execute.
Use polymorphism to handle different object types through a common interface.
It simplifies adding new behaviors without changing existing code.