Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Runtime Polymorphism in Java
π Scenario: Imagine you are creating a simple program for a zoo. Different animals make different sounds. You want to write code that can handle any animal and make it produce its sound correctly.
π― Goal: Build a Java program that demonstrates runtime polymorphism by using a base class Animal and subclasses Dog and Cat. The program will call the makeSound() method on different animal objects and show the correct sound for each.
π What You'll Learn
Create a base class Animal with a method makeSound().
Create subclasses Dog and Cat that override makeSound().
Create an array of Animal references holding Dog and Cat objects.
Use a loop to call makeSound() on each animal and print the result.
π‘ Why This Matters
π Real World
Runtime polymorphism lets programs handle different objects in a flexible way, like animals making sounds without knowing their exact type beforehand.
πΌ Career
Understanding runtime polymorphism is essential for Java developers to write clean, reusable, and maintainable code using inheritance and method overriding.
Progress0 / 4 steps
1
Create the base class Animal
Create a public class called Animal with a public method makeSound() that prints "Some generic animal sound".
Java
Hint
Define a class named Animal. Inside it, write a method makeSound() that prints the generic sound.
2
Create subclasses Dog and Cat overriding makeSound()
Create two public classes Dog and Cat that extend Animal. Override the makeSound() method in Dog to print "Woof" and in Cat to print "Meow".
Java
Hint
Use extends Animal to create subclasses. Use @Override and redefine makeSound() to print the correct sounds.
3
Create an array of Animal references holding Dog and Cat objects
Create a public class Main with a main method. Inside main, create an array of Animal called animals that holds one Dog object and one Cat object.
Java
Hint
Define a Main class with a main method. Create an array of type Animal and put new Dog() and Cat() objects inside.
4
Use a loop to call makeSound() on each animal and print the result
Inside the main method, use a for loop with variable animal to iterate over the animals array. Call animal.makeSound() inside the loop to print each animal's sound.
Java
Hint
Use a for-each loop to go through each animal in the animals array. Call animal.makeSound() inside the loop to print the sounds.
Practice
(1/5)
1. What is runtime polymorphism in Java?
easy
A. Creating multiple objects of the same class
B. Using multiple classes with the same name
C. Choosing which method to call during program execution based on object type
D. Writing methods with different names in the same class
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 C
Quick 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
A. class Parent { void show() {} } class Child extends Parent { void show() {} }
B. class Parent { void show() {} } class Child extends Parent { void display() {} }
C. class Parent { void show() {} } class Child { void show() {} }
D. class Parent { void show() {} } class Child extends Parent { void show(int x) {} }
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 overriding show() method correctly; others differ in method name or parameters.
Final Answer:
class Parent { void show() {} } class Child extends Parent { void show() {} } -> Option A
Quick 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
A. Bark
B. Animal sound
C. Compilation error
D. Runtime error
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 A
Quick 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
A. Parent class method show() is private
B. Child class does not override show() method correctly
C. Cannot assign Child object to Parent reference
D. Missing main method
Solution
Step 1: Check method overriding in Child class
Child defines show(int x), which is overloading, not overriding show().
Step 2: Understand method call on Parent reference
Parent reference calls show() 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 B
Quick 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:
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
A. Vehicle starts
Vehicle starts
Vehicle starts
B. Compilation error due to array initialization
C. Car starts
Vehicle starts
Bike starts
D. Car starts
Bike starts
Vehicle starts
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
Each start() 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 D
Quick Check:
Overridden methods run per object type in array [OK]
Hint: Loop calls overridden methods based on actual object type [OK]