0
0
Javaprogramming~5 mins

Runtime polymorphism in Java

Choose your learning style9 modes available
Introduction

Runtime polymorphism lets a program decide which method to use while it is running. This helps make programs flexible and easy to change.

When you want different objects to respond differently to the same method call.
When you want to write code that works with a general type but behaves differently for specific types.
When you want to change or extend behavior without changing existing code.
When you want to use a common interface but different implementations.
When you want to simplify code by using method overriding.
Syntax
Java
class Parent {
    void show() {
        System.out.println("Parent show method");
    }
}

class Child extends Parent {
    @Override
    void show() {
        System.out.println("Child show method");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
    }
}

The method to call is decided at runtime based on the actual object type.

Use method overriding to achieve runtime polymorphism.

Examples
Different objects (Dog, Cat) respond differently to the same method call sound().
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 Test {
    public static void main(String[] args) {
        Animal a1 = new Dog();
        Animal a2 = new Cat();
        a1.sound();
        a2.sound();
    }
}
The run() method of Bike is called even though the reference is of type Vehicle.
Java
class Vehicle {
    void run() {
        System.out.println("Vehicle is running");
    }
}

class Bike extends Vehicle {
    @Override
    void run() {
        System.out.println("Bike is running fast");
    }
}

public class Demo {
    public static void main(String[] args) {
        Vehicle v = new Bike();
        v.run();
    }
}
Sample Program

This program shows how the method greet() behaves differently depending on the actual object type at runtime.

Java
class Parent {
    void greet() {
        System.out.println("Hello from Parent");
    }
}

class Child extends Parent {
    @Override
    void greet() {
        System.out.println("Hello from Child");
    }
}

public class RuntimePolymorphismDemo {
    public static void main(String[] args) {
        Parent p = new Parent();
        Parent c = new Child();

        p.greet();  // Calls Parent's greet
        c.greet();  // Calls Child's greet because of runtime polymorphism
    }
}
OutputSuccess
Important Notes

Runtime polymorphism requires inheritance and method overriding.

The reference type decides what methods are accessible, but the object type decides which method runs.

Final, static, and private methods cannot be overridden, so they do not support runtime polymorphism.

Summary

Runtime polymorphism lets a program choose the right method to run while it is running.

It is done using method overriding in inheritance.

This helps write flexible and reusable code.