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! π