Different Types of Inheritance in .NET

๐Ÿ’ก Concept Name

Types of Inheritance in .NET

๐Ÿ“˜ Quick Intro

Inheritance is a fundamental concept in object-oriented programming that enables a class to inherit members of another class. .NET supports several types of inheritance to encourage code reuse and polymorphism.

๐Ÿง  Analogy / Short Story

Think of a car company. There's a base Car model. SportsCar and Truck inherit features from Car. Further, ElectricTruck may inherit from Truck. Just like car types evolve by inheriting common features, classes in .NET inherit functionality using inheritance.

๐Ÿ”ง Technical Explanation

  • Single Inheritance: One class inherits from one base class.
  • Multilevel Inheritance: A class derives from a class that is already derived from another.
  • Hierarchical Inheritance: Multiple classes inherit from a single base class.
  • Multiple Inheritance (via interfaces): A class can implement multiple interfaces.

๐ŸŽฏ Purpose & Use Case

  • โœ… Reuse common logic across different classes
  • โœ… Establish consistent base behavior in systems
  • โœ… Promote polymorphism and extensibility

๐Ÿ’ป 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!

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: