Why polymorphism matters in C Sharp (C#) - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
We want to see how using polymorphism affects the time it takes for a program to run.
Does choosing polymorphism change how fast the program works as it handles more data?
Analyze the time complexity of the following code snippet.
public abstract class Animal {
public abstract void Speak();
}
public class Dog : Animal {
public override void Speak() { Console.WriteLine("Woof"); }
}
public class Cat : Animal {
public override void Speak() { Console.WriteLine("Meow"); }
}
public void MakeAnimalsSpeak(List animals) {
foreach (var animal in animals) {
animal.Speak();
}
}
This code calls the Speak method on a list of animals using polymorphism.
- Primary operation: Looping through each animal and calling Speak()
- How many times: Once for each animal in the list (n times)
As the number of animals grows, the program calls Speak() more times, once per animal.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to Speak() |
| 100 | 100 calls to Speak() |
| 1000 | 1000 calls to Speak() |
Pattern observation: The number of operations grows directly with the number of animals.
Time Complexity: O(n)
This means the time to run grows in a straight line as you add more animals.
[X] Wrong: "Polymorphism makes the program slower because it adds extra steps."
[OK] Correct: The extra step is just one method call per item, so it grows linearly and does not slow down the program in a big way.
Understanding how polymorphism affects time helps you explain your design choices clearly and confidently in real projects.
"What if we added nested loops inside each Speak method? How would the time complexity change?"
Practice
Solution
Step 1: Understand polymorphism concept
Polymorphism means one method name can work with different object types.Step 2: Identify the main benefit
This makes code easier to write and maintain by reusing method names for different classes.Final Answer:
It allows one method to work with different types of objects. -> Option DQuick Check:
Polymorphism = One method, many types [OK]
- Thinking polymorphism speeds up code automatically
- Confusing polymorphism with code immutability
- Believing polymorphism forces identical class properties
Solution
Step 1: Identify polymorphic method declaration
In C#, polymorphism is often shown using virtual methods that can be overridden.Step 2: Check method options
Only 'public virtual void Draw()' allows derived classes to override and show polymorphism.Final Answer:
public virtual void Draw() { } -> Option CQuick Check:
Virtual method enables polymorphism [OK]
- Choosing static methods which cannot be overridden
- Ignoring the virtual keyword for polymorphism
- Confusing method overloading with polymorphism
class Animal { public virtual string Speak() => "..."; }
class Dog : Animal { public override string Speak() => "Woof"; }
class Cat : Animal { public override string Speak() => "Meow"; }
Animal a = new Dog();
Console.WriteLine(a.Speak());What is the output?
Solution
Step 1: Understand polymorphism with virtual and override
The variable 'a' is of type Animal but holds a Dog object. The Speak method is virtual and overridden in Dog.Step 2: Determine which Speak method runs
At runtime, the Dog's Speak method runs, returning "Woof".Final Answer:
Woof -> Option AQuick Check:
Virtual method calls overridden version [OK]
- Expecting base class method output
- Confusing variable type with object type
- Thinking code causes compile error
class Shape { public void Draw() { Console.WriteLine("Shape"); } }
class Circle : Shape { public void Draw() { Console.WriteLine("Circle"); } }
Shape s = new Circle();
s.Draw();Solution
Step 1: Check method declarations for polymorphism
Polymorphism requires the base method to be marked 'virtual' and the derived method to 'override'.Step 2: Identify missing virtual keyword
Here, Shape's Draw is not virtual, so Circle's Draw hides it but does not override.Final Answer:
Draw method in Shape should be virtual to enable polymorphism. -> Option BQuick Check:
Base method must be virtual for polymorphism [OK]
- Thinking method hiding equals polymorphism
- Believing derived method must be removed
- Assuming static methods support polymorphism
Solution
Step 1: Understand polymorphism with collections
Polymorphism allows calling the same method on base class references that point to different derived objects.Step 2: Apply to drawing shapes
Calling Draw() on each Shape in the list runs the correct overridden Draw method for each shape automatically.Final Answer:
You can call Draw() on each Shape reference and the correct shape's Draw runs automatically. -> Option AQuick Check:
Polymorphism enables method calls without type checks [OK]
- Thinking you must check types before calling methods
- Trying to cast objects unnecessarily
- Believing polymorphism doesn't work with lists
