Bird
Raised Fist0
C Sharp (C#)programming~5 mins

Runtime polymorphism execution in C Sharp (C#) - Time & Space Complexity

Choose your learning style10 modes available

Start learning this pattern below

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
Time Complexity: Runtime polymorphism execution
O(n)
Understanding Time Complexity

We want to understand how the time it takes to run a program changes when using runtime polymorphism in C#.

Specifically, we ask: How does calling methods through polymorphism affect the number of steps the program takes?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


using System;

class Animal {
    public virtual void Speak() {
        Console.WriteLine("Animal speaks");
    }
}

class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("Dog barks");
    }
}

void MakeAnimalsSpeak(Animal[] animals) {
    foreach (var animal in animals) {
        animal.Speak();
    }
}
    

This code calls the Speak method on each Animal object, using runtime polymorphism to decide which Speak version to run.

Identify Repeating Operations
  • Primary operation: The loop that calls Speak() on each animal in the array.
  • How many times: Once for each animal in the input array.
How Execution Grows With Input

Each animal in the array causes one method call. The program does a little extra work to find the right method at runtime.

Input Size (n)Approx. Operations
10About 10 method calls with runtime checks
100About 100 method calls with runtime checks
1000About 1000 method calls with runtime checks

Pattern observation: The number of operations grows directly with the number of animals. Each call has a small extra cost for deciding which method to run.

Final Time Complexity

Time Complexity: O(n)

This means the time to run grows in a straight line with the number of animals, even with runtime polymorphism.

Common Mistake

[X] Wrong: "Runtime polymorphism makes the program run much slower, like quadratic time."

[OK] Correct: Each method call still happens once per item, with only a small extra step to find the right method. It does not multiply calls or loops.

Interview Connect

Understanding how runtime polymorphism affects performance shows you know how programs decide what to do while running. This skill helps you write clear and efficient code.

Self-Check

"What if we replaced the array with a nested loop calling Speak on each animal multiple times? How would the time complexity change?"

Practice

(1/5)
1. What does runtime polymorphism in C# allow you to do?
easy
A. Create multiple instances of the same class
B. Change variable types at runtime
C. Call derived class methods through a base class reference
D. Use static methods without creating objects

Solution

  1. Step 1: Understand runtime polymorphism concept

    Runtime polymorphism allows a base class reference to call methods overridden in derived classes.
  2. Step 2: Identify correct behavior

    This means the actual method called depends on the object's real type, not the reference type.
  3. Final Answer:

    Call derived class methods through a base class reference -> Option C
  4. Quick Check:

    Runtime polymorphism = base ref calls derived method [OK]
Hint: Think: base class ref calls derived method at runtime [OK]
Common Mistakes:
  • Confusing polymorphism with changing variable types
  • Thinking static methods are polymorphic
  • Believing polymorphism creates multiple instances
2. Which keyword is used in C# to allow a method to be overridden in a derived class?
easy
A. virtual
B. override
C. new
D. abstract

Solution

  1. Step 1: Identify keyword to enable overriding

    The base class method must be marked with virtual to allow overriding.
  2. Step 2: Understand roles of keywords

    override is used in derived classes, virtual in base classes.
  3. Final Answer:

    virtual -> Option A
  4. Quick Check:

    Base method uses virtual to allow override [OK]
Hint: Base method uses virtual; derived uses override [OK]
Common Mistakes:
  • Using override in base class instead of virtual
  • Confusing new keyword with override
  • Thinking abstract is required for all overrides
3. What is the output of this C# code?
class Animal {
  public virtual string Speak() => "Animal sound";
}
class Dog : Animal {
  public override string Speak() => "Bark";
}
class Cat : Animal {
  public override string Speak() => "Meow";
}

Animal a = new Dog();
Console.WriteLine(a.Speak());
medium
A. Animal sound
B. Bark
C. Meow
D. Compile error

Solution

  1. Step 1: Identify object type and method called

    Variable a is of type Animal but references a Dog object.
  2. Step 2: Apply runtime polymorphism

    Since Speak is virtual and overridden in Dog, the Dog version runs, printing "Bark".
  3. Final Answer:

    Bark -> Option B
  4. Quick Check:

    Base ref calls Dog's Speak() = Bark [OK]
Hint: Base ref calls derived override method at runtime [OK]
Common Mistakes:
  • Expecting base class method output
  • Confusing object type with reference type
  • Thinking compile error due to override
4. Identify the error in this C# code related to runtime polymorphism:
class Base {
  public override void Show() {
    Console.WriteLine("Base Show");
  }
}
class Derived : Base {
  public override void Show() {
    Console.WriteLine("Derived Show");
  }
}
medium
A. Base class method must be virtual, not override
B. Derived class method cannot override base method
C. Missing semicolon after method declaration
D. No error, code is correct

Solution

  1. Step 1: Check base class method declaration

    Base class method incorrectly uses override instead of virtual.
  2. Step 2: Understand override rules

    Only derived classes use override; base class must use virtual to allow overriding.
  3. Final Answer:

    Base class method must be virtual, not override -> Option A
  4. Quick Check:

    Base method needs virtual keyword [OK]
Hint: Base method uses virtual, not override [OK]
Common Mistakes:
  • Using override in base class method
  • Thinking override is allowed without virtual
  • Ignoring method signature correctness
5. Given these classes:
class Vehicle {
  public virtual string Describe() => "Vehicle";
}
class Car : Vehicle {
  public override string Describe() => "Car";
}
class SportsCar : Car {
  public override string Describe() => "SportsCar";
}

Vehicle v = new SportsCar();
Car c = new SportsCar();
SportsCar s = new SportsCar();

Console.WriteLine(v.Describe());
Console.WriteLine(c.Describe());
Console.WriteLine(s.Describe());
What is the output?
hard
A. Vehicle\nCar\nSportsCar
B. Car\nCar\nCar
C. Vehicle\nVehicle\nVehicle
D. SportsCar\nSportsCar\nSportsCar

Solution

  1. Step 1: Identify actual object type for all references

    All variables v, c, and s reference a SportsCar object.
  2. Step 2: Apply runtime polymorphism for Describe()

    Since Describe is overridden in SportsCar, all calls print "SportsCar" regardless of reference type.
  3. Final Answer:

    SportsCar\nSportsCar\nSportsCar -> Option D
  4. Quick Check:

    All calls use SportsCar override [OK]
Hint: Actual object type decides method, not reference type [OK]
Common Mistakes:
  • Assuming base class method runs for base type variable
  • Confusing reference type with object type
  • Ignoring override in most derived class