Runtime polymorphism lets a program decide which method to use while it is running. This helps make programs flexible and easy to change.
Runtime polymorphism in Java
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
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(); } }
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 } }
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.
Practice
1. What is
runtime polymorphism in Java?easy
Solution
Step 1: Understand polymorphism concept
Polymorphism means many forms; in Java, it allows methods to behave differently based on object type.Step 2: Identify runtime polymorphism
Runtime polymorphism happens when the program decides which overridden method to call during execution, not before.Final Answer:
Choosing which method to call during program execution based on object type -> Option CQuick Check:
Runtime polymorphism = method choice at runtime [OK]
Hint: Runtime polymorphism means method choice happens while running [OK]
Common Mistakes:
- Confusing compile-time and runtime polymorphism
- Thinking it means multiple classes with same name
- Believing it is about method overloading
2. Which syntax correctly shows method overriding for runtime polymorphism in Java?
easy
Solution
Step 1: Check method overriding rules
Method overriding requires same method name and parameters in subclass extending superclass.Step 2: Match options with overriding
class Parent { void show() {} } class Child extends Parent { void show() {} } shows subclass overridingshow()method correctly; others differ in method name or parameters.Final Answer:
class Parent { void show() {} } class Child extends Parent { void show() {} } -> Option AQuick Check:
Same method name and parameters in subclass = overriding [OK]
Hint: Overriding needs same method name and parameters in subclass [OK]
Common Mistakes:
- Changing method name in subclass instead of overriding
- Changing method parameters (overloading, not overriding)
- Not extending the parent class
3. What is the output of this code?
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}medium
Solution
Step 1: Understand object and reference types
Reference is of type Animal, but object is Dog, so overridden method in Dog is called.Step 2: Identify method called at runtime
Due to runtime polymorphism,sound()of Dog runs, printing "Bark".Final Answer:
Bark -> Option AQuick Check:
Overridden method runs based on object type [OK]
Hint: Method called depends on object type, not reference type [OK]
Common Mistakes:
- Thinking reference type decides method called
- Expecting superclass method output
- Confusing compile-time and runtime behavior
4. Find the error in this code related to runtime polymorphism:
class Parent {
void show() { System.out.println("Parent"); }
}
class Child extends Parent {
void show(int x) { System.out.println("Child " + x); }
}
public class Test {
public static void main(String[] args) {
Parent p = new Child();
p.show();
}
}medium
Solution
Step 1: Check method overriding in Child class
Child definesshow(int x), which is overloading, not overridingshow().Step 2: Understand method call on Parent reference
Parent reference callsshow()with no arguments, but Child has no overriding method, so Parent's method runs.Final Answer:
Child class does not override show() method correctly -> Option BQuick Check:
Overriding needs exact method signature match [OK]
Hint: Overriding needs same method signature, not just same name [OK]
Common Mistakes:
- Thinking overloading is overriding
- Expecting Child's show(int) to override show()
- Ignoring method parameters in overriding
5. Given these classes:
What is the output when this program runs?
class Vehicle {
void start() { System.out.println("Vehicle starts"); }
}
class Car extends Vehicle {
void start() { System.out.println("Car starts"); }
}
class Bike extends Vehicle {
void start() { System.out.println("Bike starts"); }
}
public class Test {
public static void main(String[] args) {
Vehicle[] vehicles = {new Car(), new Bike(), new Vehicle()};
for (Vehicle v : vehicles) {
v.start();
}
}
}What is the output when this program runs?
hard
Solution
Step 1: Analyze array elements and their types
Array holds objects: Car, Bike, Vehicle, all as Vehicle references.Step 2: Understand method calls in loop
Eachstart()call runs overridden method of actual object type due to runtime polymorphism.Step 3: Determine output lines
Car prints "Car starts", Bike prints "Bike starts", Vehicle prints "Vehicle starts" in order.Final Answer:
Car starts Bike starts Vehicle starts -> Option DQuick Check:
Overridden methods run per object type in array [OK]
Hint: Loop calls overridden methods based on actual object type [OK]
Common Mistakes:
- Expecting all calls to run Vehicle's method
- Confusing array reference type with object type
- Thinking array initialization causes error
