Different Types of Inheritance in .NET
π‘ Concept: Types of Inheritance in .NET
Inheritance is one of those "superpowers" of object-oriented programming. Itβs how you teach a class to automatically pick up traits, behaviors, and rules from another classβjust like kids inherit traits from their parents π¨βπ©βπ§. In .NET, inheritance comes in different flavors, each designed to help you reuse code and build cleaner, extensible systems.
π Quick Intro
At its core, inheritance is about sharing and extending behavior. Instead of rewriting the same logic in multiple places, you define it once in a base class and let child classes reuse or override it. .NET supports different types of inheritance so developers can model relationships the way real-world systems evolve β from simple single-level to more complex multi-level setups.
π§ Analogy / Short Story
Imagine a car company. π - First, they create a basic Car model with common features (wheels, engine, brakes). - Then they roll out a SportsCar ποΈ (inherits Carβs features but adds turbo). - They also release a Truck π (inherits Carβs features but adds load capacity). - Later, they design an ElectricTruck β‘π (inherits from Truck but changes the fuel system). Each new model doesnβt reinvent the wheel (literally) β it just extends whatβs already there. Thatβs exactly how inheritance works in .NET: you build once, reuse everywhere, and evolve cleanly.
π§ Technical Breakdown
-
Single Inheritance: One class inherits from one base class.
Example:
Dog : Animal
. -
Multilevel Inheritance: A chain of inheritance where each class builds on the previous.
Example:
Puppy : Dog : Animal
. -
Hierarchical Inheritance: Multiple classes share the same parent.
Example:
Cat : Animal
,Dog : Animal
,Bird : Animal
. -
Multiple Inheritance (via interfaces): .NET doesnβt allow multiple base classes (to avoid the βdiamond problemβ), but you can implement multiple interfaces.
Example:
FlyingCar : IVehicle, IAircraft
.
π― Purpose & Use Cases
- β Code reuse: Common logic (like logging, validation, or base properties) lives in one place and spreads naturally to child classes.
- β Consistency: Define standard rules/behavior in a base class, so all child classes follow the same contract.
- β Polymorphism & Extensibility: Allows writing flexible code that can work with multiple derived types interchangeably.
- β Cleaner evolution: As your system grows, you extend existing functionality instead of duplicating it.
π» Real Code Example
public class Animal {
public void Eat() => Console.WriteLine("Eating...");
}
public class Dog : Animal {
public void Bark() => Console.WriteLine("Barking...");
}
public class Puppy : Dog {
public void Weep() => Console.WriteLine("Weeping...");
}
public interface IWalkable { void Walk(); }
public interface IRunable { void Run(); }
public class Person : IWalkable, IRunable {
public void Walk() => Console.WriteLine("Walking");
public void Run() => Console.WriteLine("Running");
}

β Interview Q&A
Q1: What is inheritance in .NET?
A: A mechanism to derive new classes from existing ones for code reuse.
Q2: Does .NET support multiple inheritance via classes?
A: No, but supports via interfaces.
Q3: What is hierarchical inheritance?
A: Multiple classes inherit from a single base class.
Q4: Why use interfaces for multiple inheritance?
A: Interfaces allow type abstraction and multiple inheritance.
Q5: Can a class inherit both class and interface?
A: Yes, single class + multiple interfaces.
Q6: Which inheritance supports polymorphism?
A: Single, multilevel and interface-based inheritance.
Q7: Is constructor inherited in C#?
A: No, constructors are not inherited.
Q8: Can base class methods be overridden?
A: Yes, if marked virtual or abstract.
Q9: How to prevent inheritance in C#?
A: Use sealed keyword.
Q10: Can interfaces have default implementations?
A: Yes, from C# 8.0 onwards.
π MCQs
Q1. Which type of inheritance is supported directly in C#?
- Multiple Inheritance
- Diamond Inheritance
- Single Inheritance
- Hybrid Inheritance
Q2. How can .NET achieve multiple inheritance?
- Abstract Classes
- Delegates
- Using Interfaces
- Partial Classes
Q3. What does 'sealed' keyword do?
- Makes class abstract
- Prevents a class from being inherited
- Allows multiple inheritance
- Disables constructors
Q4. Which inheritance allows a class to extend a class that’s already derived?
- Single
- Multilevel
- Hierarchical
- None
Q5. Can a class inherit from more than one base class in .NET?
- Yes
- No
- Only abstract
- Only sealed
Q6. What does interface inheritance enable?
- Overriding
- Constructors
- Multiple inheritance
- Method hiding
Q7. Which of these is an example of hierarchical inheritance?
- Car inherits from Vehicle
- Dog and Cat inherit from Animal
- Puppy inherits from Dog
- None
Q8. What cannot be inherited in C#?
- Methods
- Properties
- Fields
- Constructors
Q9. Which keyword is used to prevent method override?
- const
- readonly
- sealed
- abstract
Q10. Which of the following supports polymorphism in .NET?
- Encapsulation
- Overloading
- Inheritance
- Constructor chaining
π‘ Bonus Insight
Although multiple inheritance through classes is not supported, interface-based inheritance provides a safe and powerful alternative without the diamond problem.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!