Loading...

Inheritance and Polymorphism in C#

Imagine walking through a zoo. Every animal can eat and sleep, but each one speaks differently. A lion roars, a cat meows, a dog barks. They share some behavior but express it uniquely.

This is exactly how inheritance and polymorphism work in C#:
you define common features once, and each object can still behave in its own special way.

Today, you’ll learn these powerful concepts and build a fun Animal Speaking Simulator.

Understanding Inheritance

Inheritance lets one class reuse the properties and methods of another.
Instead of writing the same code repeatedly, your derived classes simply “extend” the base class.

Example:

csharp
7 lines
|
24/ 500 tokens
1
2
3
4
5
6
7
class Animal
{
    public void Eat() => Console.WriteLine("Eating...");
}
class Dog : Animal
{
}
Code Tools

Now Dog automatically has Eat(), no extra writing needed. Inheritance reduces repetition and keeps your code clean and organized.

Understanding Polymorphism

Polymorphism means “many forms.”
It allows different classes to use the same method name but behave differently.

For example:

  • Dog → Bark
  • Cat → Meow
  • Lion → Roar

Even though they all share the same Speak() method, each one expresses it differently.

virtual and override

To enable polymorphism, C# uses two keywords:

  • virtual → allows a method in the base class to be changed
csharp
1 lines
|
8/ 500 tokens
1
public virtual void Speak() { }
Code Tools
  • override → provides the new version in the derived class
csharp
1 lines
|
8/ 500 tokens
1
public override void Speak() { }
Code Tools

This keeps your design flexible and each class meaningful.

Mini-Project: Animal Speaking Simulator

Let’s apply everything by building a simple zoo simulation where animals speak in their own voices.

csharp
49 lines
|
206/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;

class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("This animal makes a sound.");
    }
}

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

class Cat : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Cat says: Meow!");
    }
}

class Lion : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Lion says: Roar!");
    }
}

class Program
{
    static void Main()
    {
        Animal[] animals = { new Dog(), new Cat(), new Lion() };

        Console.WriteLine("Animal Speaking Simulator:");
        Console.WriteLine("==========================");

        foreach (Animal a in animals)
        {
            a.Speak();  // polymorphism in action
        }
    }
}
Code Tools

With one loop and one method call, every animal expresses itself differently, pure polymorphism.

How It Works

  • The base class defines a general template
  • Derived classes override behavior where needed
  • A single Speak() call triggers different results
  • Your program becomes flexible and easy to extend

Try adding more animals in the DevsCall AI Online Runner and watch the zoo grow.

Learn Together with AI

Use these prompts to explore further:

  • “Add a Bird class that chirps.”
  • “Create a menu where the user chooses an animal to speak.”
  • “Explain virtual and override with even simpler examples.”

AI helps you expand your OOP skills naturally.

Practice Time

Create:

  • A Vehicle base class
  • Derived classes: Car, Bicycle, Plane
  • Override a Move() method to describe each movement

Expected output:

bash
3 lines
|
18/ 500 tokens
1
2
3
Car moves on roads.
Bicycle moves on two wheels.
Plane flies in the sky.
Code Tools

Wrap Up

Inheritance lets you avoid rewriting code. Polymorphism lets your objects act uniquely while sharing the same interface. Together, they form the backbone of real-world object-oriented programming.

Frequently Asked Questions

Inheritance allows one class to reuse the fields and methods of another class, reducing code duplication and improving structure.

A base class is the parent class whose properties and behaviors are shared by derived (child) classes.

A derived class extends the base class and can add new features or modify existing behavior.

Constructors are not inherited, but derived classes can call base class constructors using base().

Still have questions?Contact our support team