Types of Inheritance in C# – Explained with Simple Examples
👋 Introduction – What is Inheritance?
Hey, buddy! Ever noticed how a baby inherits features from their parents? Like eye color, hair type, or even personality traits?
In C# inheritance, one class inherits properties and behaviors from another class. This helps us reuse code instead of writing the same thing again and again.
📚 What You Are Going to Learn in This Lesson:
✔️ What is inheritance?
✔️ Why do we need it in C#?
✔️ Types of Inheritance in C# – With simple, easy-to-understand examples
✔️ Step-by-step coding examples with output
Sounds fun? Let’s get started! 🚀
🔥 Why Do We Use Inheritance?
Imagine you are designing a game with different types of animals 🦁🐶🐦.
Every animal has:
- A name
- A sound
- A way to move
Instead of writing this again for every animal, we create a base class (Animal) and let other animals inherit from it.
Now, let’s explore the types of inheritance in C# with simple examples!
Types of Inheritance in C#
1️⃣ Single Inheritance – One Parent, One Child
A child class inherits from a single parent class.
🦁 Example – Animal and Dog
using System;
class Animal // Parent Class
{
public string Name;
public void MakeSound()
{
Console.WriteLine($"{Name} makes a sound.");
}
}
// Dog class inherits from Animal
class Dog : Animal
{
public void Bark()
{
Console.WriteLine($"{Name} barks: Woof Woof!");
}
}
class Program
{
static void Main()
{
Dog myDog = new Dog();
myDog.Name = "Buddy";
myDog.MakeSound(); // Inherited from Animal
myDog.Bark(); // Defined in Dog class
}
}
🖥️ Output:
Buddy makes a sound.
Buddy barks: Woof Woof!
🎯 Key Takeaway: The Dog class reuses the MakeSound() method from Animal. This is single inheritance in action!
2️⃣ Multilevel Inheritance – Parent ➡ Child ➡ Grandchild
A child inherits from a parent, and another child inherits from the first child.
🚗 Example – Vehicle, Car, and Electric Car
using System;
class Vehicle // Base Class
{
public void Start()
{
Console.WriteLine("Vehicle is starting...");
}
}
// Car inherits from Vehicle
class Car : Vehicle
{
public void Drive()
{
Console.WriteLine("Car is driving...");
}
}
// ElectricCar inherits from Car
class ElectricCar : Car
{
public void ChargeBattery()
{
Console.WriteLine("Electric car is charging...");
}
}
class Program
{
static void Main()
{
ElectricCar myTesla = new ElectricCar();
myTesla.Start(); // Inherited from Vehicle
myTesla.Drive(); // Inherited from Car
myTesla.ChargeBattery(); // Defined in ElectricCar
}
}
🖥️ Output:
Vehicle is starting...
Car is driving...
Electric car is charging...
🎯 Key Takeaway: In multilevel inheritance, a child class inherits from another child class.
3️⃣ Hierarchical Inheritance – One Parent, Multiple Children
One parent class is inherited by multiple child classes.
🐦🐕 Example – Animal, Dog, and Bird
using System;
class Animal // Parent Class
{
public string Name;
public void Eat()
{
Console.WriteLine($"{Name} is eating...");
}
}
// Dog inherits from Animal
class Dog : Animal
{
public void Bark()
{
Console.WriteLine($"{Name} barks: Woof Woof!");
}
}
// Bird inherits from Animal
class Bird : Animal
{
public void Fly()
{
Console.WriteLine($"{Name} is flying!");
}
}
class Program
{
static void Main()
{
Dog myDog = new Dog();
myDog.Name = "Rex";
myDog.Eat(); // Inherited from Animal
myDog.Bark(); // Defined in Dog
Bird myBird = new Bird();
myBird.Name = "Tweety";
myBird.Eat(); // Inherited from Animal
myBird.Fly(); // Defined in Bird
}
}
🖥️ Output:
Rex is eating...
Rex barks: Woof Woof!
Tweety is eating...
Tweety is flying!
🎯 Key Takeaway: One parent (Animal), multiple children (Dog and Bird).
Inheritance That C# Doesn’t Support ❌
❌ Multiple Inheritance (One class inheriting from two parents) is not allowed in C#. Instead, we use Interfaces to achieve similar functionality!
💡 Benefits of Inheritance in C#
✔️ Avoids duplicate code – Write once, use multiple times!
✔️ Better organization – Makes code easy to manage.
✔️ Scalability – Add new features without modifying existing ones!
🚀 Conclusion
✅ You now understand the types of inheritance in C#.
✅ You saw simple and fun examples (Animals, Vehicles, etc.).
✅ You know why inheritance is super useful in programming!
Great job! 🎉 Keep practicing, and soon you’ll master object-oriented programming in C#!
👉 Next What?
In the next lesson, you’ll learn about Access Specifiers in Inheritance in C# – public, private, and protected inheritance with cool examples!
🔥 Stay tuned! 🚀 Let me know if you have any questions! 😊
