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

Runtime polymorphism execution in C Sharp (C#)

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
Introduction

Runtime polymorphism lets a program decide which method to run while it is running, not before. This helps make programs flexible and easy to change.

When you want different objects to respond differently to the same action.
When you have a group of related objects but each behaves in its own way.
When you want to add new behaviors without changing existing code.
When you want to write code that works with many types of objects easily.
Syntax
C Sharp (C#)
class BaseClass {
    public virtual void Show() {
        // base method
    }
}

class DerivedClass : BaseClass {
    public override void Show() {
        // derived method
    }
}

BaseClass obj = new DerivedClass();
obj.Show();

virtual keyword marks a method that can be changed in child classes.

override keyword is used in child classes to provide a new version of the method.

Examples
Here, the Speak method is called on an Animal reference but runs the Dog version because of runtime polymorphism.
C Sharp (C#)
class Animal {
    public virtual void Speak() {
        Console.WriteLine("Animal speaks");
    }
}

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

Animal pet = new Dog();
pet.Speak();
The Draw method runs the Circle version even though the variable is of type Shape.
C Sharp (C#)
class Shape {
    public virtual void Draw() {
        Console.WriteLine("Drawing shape");
    }
}

class Circle : Shape {
    public override void Draw() {
        Console.WriteLine("Drawing circle");
    }
}

Shape s = new Circle();
s.Draw();
Sample Program

This program shows runtime polymorphism by calling the Start method on a Vehicle reference that points to different types of vehicles. The actual method that runs depends on the object type at runtime.

C Sharp (C#)
using System;

class Vehicle {
    public virtual void Start() {
        Console.WriteLine("Vehicle is starting");
    }
}

class Car : Vehicle {
    public override void Start() {
        Console.WriteLine("Car is starting with a roar");
    }
}

class Bike : Vehicle {
    public override void Start() {
        Console.WriteLine("Bike is starting quietly");
    }
}

class Program {
    static void Main() {
        Vehicle myVehicle;

        myVehicle = new Car();
        myVehicle.Start();

        myVehicle = new Bike();
        myVehicle.Start();
    }
}
OutputSuccess
Important Notes

Runtime polymorphism requires a base method marked with virtual and derived methods marked with override.

The decision about which method to call happens when the program runs, not when it is compiled.

Using runtime polymorphism helps keep code easy to extend and maintain.

Summary

Runtime polymorphism lets methods behave differently based on the actual object type at runtime.

Use virtual in base classes and override in derived classes to enable this behavior.

This makes your code flexible and easier to change later.

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