Thuta Learning
C#
AdvancedProgrammingBeginner

Polymorphism

C#Lesson 2025 min

What you'll walk away with

  • Explain the core concept and runtime behavior of Polymorphism
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

Explain it simply

Polymorphism, in plain language

Polymorphism means different objects can respond in their own way when you call a method with the same name on them. You mark the method virtual in the base class, and derived classes can override it.

Problem → Why → Solution

The problem — why this concept matters

Repeated type-based if/else chains can signal a missing polymorphic contract.

A common mistake

Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Core concept — understand how it works

Polymorphism lets one caller work with multiple runtime implementations through a common abstraction. Distinguish virtual and interface dispatch from overload resolution and manual type tests.

Mental model
  1. object or dependency
  2. C# type contract and CLR
  3. reusable behavior

Explain the core concept and runtime behavior of Polymorphism

Core syntax or pattern

csharp
class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The dog says: woof");
    }
}

class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The cat says: meow");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.MakeSound();
        myCat.MakeSound();
    }
}

Polymorphism lets one caller work with multiple runtime implementations through a common abstraction. Distinguish virtual and interface dispatch from overload resolution and manual type tests.

Read the code step by step

  1. object or dependencyclass Animal

    Polymorphism means different objects can respond in their own way when you call a method with the same name on them. You mark the method virtual in the base class, and derived classes can override it.

  2. C# type contract and CLR{

    Polymorphism lets one caller work with multiple runtime implementations through a common abstraction. Distinguish virtual and interface dispatch from overload resolution and manual type tests.

  3. reusable behavior}

    Put two notification implementations in one interface-typed list and send without the caller knowing concrete types.

Execution flow
  1. object or dependency
  2. C# type contract and CLR
  3. reusable behavior

Success

Explain the core concept and runtime behavior of Polymorphism

Failure

Repeated type-based if/else chains can signal a missing polymorphic contract.

Polymorphism means different objects can respond in their own way when you call a method with the same name on them. You mark the method virtual in the base class, and derived classes can override it.

csharp
class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The dog says: woof");
    }
}

class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The cat says: meow");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.MakeSound();
        myCat.MakeSound();
    }
}

What to watch out for

  • Animal myDog = new Dog(); holds it as the base type, but the method that actually runs is the override defined in Dog.
  • This concept is a lifesaver for patterns like plugin systems, payment providers, and notification channels.
You should see
The dog says: woof The cat says: meow

Real-world usage

How to apply it in a real project

Put two notification implementations in one interface-typed list and send without the caller knowing concrete types.

Mini challenge

Build it yourself

Put two notification implementations in one interface-typed list and send without the caller knowing concrete types.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
csharp
class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The dog says: woof");
    }
}

class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The cat says: meow");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.MakeSound();
        myCat.MakeSound();
    }
}

Expected: Explain the core concept and runtime behavior of Polymorphism

Show hint

Put two notification implementations in one interface-typed list and send without the caller knowing concrete types.

Quick quiz 1 / 3

What best describes the core idea of Polymorphism?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • object or dependency → C# type contract and CLR → reusable behavior
  • Polymorphism lets one caller work with multiple runtime implementations through a common abstraction. Distinguish virtual and interface dispatch from overload resolution and manual type tests.
  • Repeated type-based if/else chains can signal a missing polymorphic contract.
  • Put two notification implementations in one interface-typed list and send without the caller knowing concrete types.

Object-oriented programming in C# — Microsoft Learn

Easy traps

  • Repeated type-based if/else chains can signal a missing polymorphic contract.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Put two notification implementations in one interface-typed list and send without the caller knowing concrete types.

You'll know it worked when: The dog says: woof The cat says: meow

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Polymorphism | Thuta Learning