Inheritance is a core concept of object-oriented programming (OOP) where one class (called a subclass or derived class) can inherit properties and behaviors from another class (called a superclass or base class). This allows code to be reused, promotes modularity, and establishes relationships between classes. While the subclass inherits the fields and methods of the superclass, it can also extend or modify them, demonstrating another OOP concept: polymorphism.
Key Terminologies in Inheritance
- Super Class (Base Class or Parent Class):
The class that provides methods and fields for another class to inherit. - Sub Class (Derived Class or Child Class):
The class that inherits methods and fields from a superclass. A subclass can also have its own unique methods and fields. - Reusability:
Inheritance supports reusability by allowing existing classes to be extended. Instead of rewriting code, you can use the functionality of a base class in new derived classes.
Types of Inheritance in C#
There are five types of inheritance, each serving specific use cases:

1. Single Inheritance
A single subclass inherits from one superclass.

Example:
using System;
// Base Class
class Animal
{
public void Eat()
{
Console.WriteLine("This animal can eat.");
}
}
// Derived Class
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("This dog can bark.");
}
}
class Program
{
static void Main()
{
Dog myDog = new Dog();
myDog.Eat(); // Method from the base class
myDog.Bark(); // Method from the derived class
}
}
2. Multilevel Inheritance
A chain of inheritance where a class inherits from a derived class.

Example:
using System;
// Base Class
class Animal
{
public void Eat()
{
Console.WriteLine("This animal can eat.");
}
}
// Intermediate Derived Class
class Mammal : Animal
{
public void Walk()
{
Console.WriteLine("This mammal can walk.");
}
}
// Derived Class
class Dog : Mammal
{
public void Bark()
{
Console.WriteLine("This dog can bark.");
}
}
class Program
{
static void Main()
{
Dog myDog = new Dog();
myDog.Eat(); // Method from Animal
myDog.Walk(); // Method from Mammal
myDog.Bark(); // Method from Dog
}
}
3. Hierarchical Inheritance
Multiple subclasses inherit from a single superclass.

Example:
using System;
// Base Class
class Animal
{
public void Eat()
{
Console.WriteLine("This animal can eat.");
}
}
// Derived Class 1
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("This dog can bark.");
}
}
// Derived Class 2
class Cat : Animal
{
public void Meow()
{
Console.WriteLine("This cat can meow.");
}
}
class Program
{
static void Main()
{
Dog myDog = new Dog();
myDog.Eat();
myDog.Bark();
Cat myCat = new Cat();
myCat.Eat();
myCat.Meow();
}
}
4. Multiple Inheritance (Not directly supported in C#)
A class inherits from more than one superclass.
- Note: In C#, multiple inheritance is achieved using interfaces to avoid ambiguity.

Example (Using Interfaces):
using System;
interface IAnimal
{
void Eat();
}
interface IMammal
{
void Walk();
}
class Dog : IAnimal, IMammal
{
public void Eat()
{
Console.WriteLine("This dog can eat.");
}
public void Walk()
{
Console.WriteLine("This dog can walk.");
}
}
class Program
{
static void Main()
{
Dog myDog = new Dog();
myDog.Eat();
myDog.Walk();
}
}
5. Hybrid Inheritance
A combination of multiple and hierarchical inheritance.
- Note: Since multiple inheritance is not supported directly, hybrid inheritance can also be implemented using interfaces in C#.

Example:
using System;
interface IAnimal
{
void Eat();
}
class Mammal
{
public void Walk()
{
Console.WriteLine("This mammal can walk.");
}
}
class Dog : Mammal, IAnimal
{
public void Eat()
{
Console.WriteLine("This dog can eat.");
}
public void Bark()
{
Console.WriteLine("This dog can bark.");
}
}
class Program
{
static void Main()
{
Dog myDog = new Dog();
myDog.Eat();
myDog.Walk();
myDog.Bark();
}
}
Advantages of Inheritance
- Code Reusability: Eliminates redundancy by allowing reuse of existing code.
- Modularity: Promotes a modular structure where specific functionalities are divided across classes.
- Ease of Maintenance: Simplifies code updates by focusing on base or derived classes.
- Extensibility: Allows subclasses to extend or modify the behavior of parent classes.