0
0
JavaConceptBeginner · 4 min read

Runtime Polymorphism in Java: What It Is and How It Works

In Java, runtime polymorphism means that a call to an overridden method is resolved at runtime, not compile time. It allows a parent class reference to point to a child class object and invoke the child's version of the method dynamically.
⚙️

How It Works

Runtime polymorphism in Java happens through method overriding. Imagine you have a remote control (the parent class reference) that can operate different devices (child class objects). When you press a button, the actual device decides what happens, not the remote itself.

This means the Java program decides which method to run only when the program is running, based on the actual object type. This is different from compile time, where decisions are made before the program runs.

It helps programs be flexible and easy to extend because you can add new device types without changing the remote control code.

💻

Example

This example shows a parent class Animal with a method sound(). Two child classes Dog and Cat override this method. At runtime, the correct sound() method runs depending on the actual object.

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 runtime polymorphism when you want your program to choose behavior dynamically based on the actual object type. It is useful in situations like:

  • Designing flexible systems where new types can be added easily without changing existing code.
  • Implementing interfaces or abstract classes where different classes provide their own behavior.
  • Writing code that works with general types but behaves specifically for each subtype.

For example, in a drawing app, you can have a general Shape class and many specific shapes like Circle and Rectangle. You can call draw() on any shape, and the right shape’s drawing method runs automatically.

Key Points

  • Runtime polymorphism is achieved by method overriding in Java.
  • The method to call is decided at runtime based on the object type.
  • It requires a parent class reference pointing to a child class object.
  • It helps write flexible and extensible code.
  • It is a core concept of object-oriented programming.

Key Takeaways

Runtime polymorphism lets Java decide which overridden method to call while the program runs.
It requires a parent class reference to refer to a child class object.
Use it to write flexible code that can work with different object types dynamically.
Method overriding is the mechanism behind runtime polymorphism.
It supports extensibility and cleaner code design in object-oriented programming.