Lesson 7 — Inheritance in C#

Inheritance is the third pillar of Object-Oriented Programming (OOP) and one of the most powerful features of C#.
It allows one class to acquire the properties and behaviors of another class.

In simple words:

Inheritance = Reusing code by creating a parent–child relationship between classes.

This enables cleaner, reusable, and maintainable code.


🌟 What is Inheritance?

Inheritance allows a class (called child or derived class) to use the members of another class (called parent or base class).

✔ Simple Definition:

A derived class inherits fields, properties, and methods from a base class.


🔍 Basic Syntax of Inheritance in C#

class BaseClass
{
    // Parent features
}

class DerivedClass : BaseClass
{
    // Child features
}

The symbol : (colon) is used to inherit from a class.


🚗 Real-World Analogy of Inheritance

🔹 Vehicle → Car → ElectricCar

  • Vehicle (Base class)
    Properties: brand, model
    Methods: Start(), Stop()
  • Car (Derived class)
    Inherits Vehicle
    Adds: Doors, FuelType
  • ElectricCar (Derived class)
    Inherits Car
    Adds: BatteryCapacity

This chain shows how features are inherited from parent to child.


💡 C# Example — Simple Inheritance

🛠 Base Class

public class Animal
{
    public void Eat()
    {
        Console.WriteLine("Eating...");
    }
}

🛠 Derived Class

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

🛠 Using Inheritance

Dog d = new Dog();
d.Eat();   // inherited from Animal
d.Bark();  // belongs to Dog

Dog has both Eat() and Bark()
✔ Reusability achieved


🌈 Types of Inheritance in C#


1️⃣ Single Inheritance

One child inherits from one parent.

class A { }
class B : A { }


2️⃣ Multilevel Inheritance

Parent → Child → Grandchild

class A { }
class B : A { }
class C : B { }


3️⃣ Hierarchical Inheritance

One parent → Multiple children

class Animal { }
class Dog : Animal { }
class Cat : Animal { }
class Horse : Animal { }


❌ Note: C# does NOT support Multiple Inheritance

A class CANNOT inherit from multiple classes.

// Not allowed
class A : B, C { }

But this is solved using interfaces (covered in Lesson 16).


🧠 Why Use Inheritance?

✔ Code Reusability

Write common code once → reuse in multiple children.

✔ Cleaner Code

Organize related classes in a hierarchy.

✔ Extensibility

Add new child classes without modifying base class.

✔ Polymorphism Support

Inheritance is required for method overriding.


🧱 Practical Example — Employee Inheritance

🔹 Base Class

public class Employee
{
    public string Name;
    public double Salary;

    public void Work()
    {
        Console.WriteLine($"{Name} is working...");
    }
}

🔹 Derived Class (Full-Time Employee)

public class FullTimeEmployee : Employee
{
    public double Bonus;
}

🔹 Derived Class (Part-Time Employee)

public class PartTimeEmployee : Employee
{
    public int HoursWorked;
}

🔹 Using the Hierarchy

FullTimeEmployee f = new FullTimeEmployee();
f.Name = "Rohan";
f.Salary = 50000;
f.Bonus = 2000;
f.Work();

PartTimeEmployee p = new PartTimeEmployee();
p.Name = "John";
p.Salary = 20000;
p.HoursWorked = 40;
p.Work();

✔ Both classes reuse Work()
✔ Each class adds unique features
✔ This shows hierarchical inheritance


🔄 Overriding Methods Using Inheritance (Simple Polymorphism)

You can override a method in a derived class:

public class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("Some sound");
    }
}

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


🎯 Important Keywords Used in Inheritance

KeywordMeaning
baseAccess parent class members
virtualMakes a method overridable
overrideOverrides parent class method
sealedPrevents further inheritance

Example:

public class Person
{
    public virtual void Speak() { }
}

public class Student : Person
{
    public override void Speak() { }
}


⚠ When NOT to Use Inheritance

Avoid inheritance when:

❌ Classes are unrelated
❌ Code becomes too tightly coupled
❌ Composition fits better (HAS-A relationship)

If “IS-A” works → use inheritance
If “HAS-A” works → use composition

Example:
A Car has a Engine → NOT inheritance
A Car is a Vehicle → inheritance


📝 Mini Exercise

Create the following inheritance structure:

Parent Class:

Shape

  • Method: Area()

Child Classes:

Circle and Rectangle

Implement:

Area of circle: πr²  
Area of rectangle: length × width

Then print the area of both shapes.


🔍 FAQs

Q1: Does C# support multiple inheritance?

No, but you can achieve it using interfaces.

Q2: Can constructors be inherited?

Constructors are not inherited, but the base constructor can be called.

Q3: Is inheritance mandatory in OOP?

Not always. Use inheritance when there’s a clear IS-A relationship.

Q4: What happens if two classes share the same method name?

Overriding or hiding occurs depending on keywords used.


🎉 Conclusion

Inheritance is a powerful pillar of OOP that allows code reuse, cleaner design, and easier expansion.
Understanding parent–child relationships in C# helps you write modular and scalable applications.

You now know:

✔ How inheritance works
✔ Types of inheritance
✔ Real-world examples
✔ C# syntax and examples
✔ When to use or avoid inheritance