Why polymorphism is needed in Java - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how using polymorphism affects the time it takes for a program to run.
Specifically, does choosing polymorphism change how the program's work grows as input grows?
Analyze the time complexity of the following code snippet.
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() { System.out.println("Bark"); }
}
class Cat implements Animal {
public void sound() { System.out.println("Meow"); }
}
public class Farm {
public void makeSounds(Animal[] animals) {
for (Animal a : animals) {
a.sound();
}
}
}
This code uses polymorphism to call the correct sound method for each animal in a list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array of animals and calling their sound method.
- How many times: Once for each animal in the array.
Each animal in the list causes one call to sound().
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to sound() |
| 100 | 100 calls to sound() |
| 1000 | 1000 calls to sound() |
Pattern observation: The work grows directly with the number of animals.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of animals.
[X] Wrong: "Polymorphism makes the program slower because it adds extra steps."
[OK] Correct: The extra step is very small and does not change how the total work grows with input size.
Understanding how polymorphism affects time helps you explain design choices clearly and confidently.
"What if we replaced the array with a linked list? How would the time complexity change?"
Practice
Solution
Step 1: Understand polymorphism concept
Polymorphism means one name can represent many forms, especially methods or objects.Step 2: Identify its purpose in Java
It allows writing code that works with different types through a common interface.Final Answer:
To allow one interface to be used for different data types -> Option AQuick Check:
Polymorphism = One interface, many types [OK]
- Thinking polymorphism speeds up execution
- Confusing polymorphism with code size reduction
- Believing it limits device compatibility
Solution
Step 1: Check object assignment compatibility
Polymorphism allows a superclass reference to point to a subclass object, like Animal a = new Dog();Step 2: Verify method call correctness
Calling a method on the superclass reference that is overridden in subclass shows polymorphism.Final Answer:
Animal a = new Dog(); a.sound(); -> Option DQuick Check:
Superclass ref to subclass object = polymorphism [OK]
- Assigning superclass object to subclass reference
- Using subclass-specific methods on superclass reference
- Ignoring method overriding in polymorphism
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Dog barks"); }
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}Solution
Step 1: Identify polymorphic call
Variable 'a' is of type Animal but refers to a Dog object.Step 2: Determine method execution
At runtime, Dog's overridden sound() method is called, printing "Dog barks".Final Answer:
Dog barks -> Option CQuick Check:
Overridden method runs based on object type [OK]
- Expecting superclass method output
- Confusing compile-time and runtime method binding
- Thinking code causes errors
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void bark() { System.out.println("Dog barks"); }
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.bark();
}
}Solution
Step 1: Check reference type methods
Variable 'a' is of type Animal, which does not have method bark().Step 2: Understand method call rules
At compile time, only methods in Animal class are accessible via 'a'. bark() is undefined there.Final Answer:
Method bark() is not found in Animal class -> Option BQuick Check:
Reference type limits accessible methods [OK]
- Thinking subclass methods are always accessible
- Ignoring compile-time method checking
- Assuming override annotation is mandatory
Solution
Step 1: Understand polymorphism's role in code flexibility
Polymorphism allows new subclasses to be created that fit existing interfaces.Step 2: See how it affects maintenance and extension
Existing code can use new classes without modification, making programs easier to grow and maintain.Final Answer:
By allowing new classes to be added with minimal changes to existing code -> Option AQuick Check:
Polymorphism enables easy extension [OK]
- Thinking polymorphism forces identical method code
- Believing it stops program changes
- Assuming it improves speed directly
