Complete C# Tutorial

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.

πŸ”₯ 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! 😊

Leave a Comment

Share this Doc

Types of Inheritance

Or copy link