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!

Learn More about .NET Core

Share:

Tags:


Feedback Modal Popup