Does .NET Support Multiple Inheritance?

πŸ’‘ Concept Name

Multiple Inheritance in .NET

πŸ“˜ Quick Intro

In .NET, a class can only inherit from one base class. This is known as single inheritance. However, .NET supports multiple interface inheritance, where a class can implement multiple interfaces.

🧠 Analogy / Short Story

Imagine you're a student trying to attend two schools at the same timeβ€”each giving different rules for the same subject. Which do you follow? That's the problem multiple class inheritance can causeβ€”confusion and conflict. Interfaces, on the other hand, are like guidelines: they tell you what to do, but you decide how.

πŸ”§ Technical Explanation

.NET and C# avoid multiple class inheritance to prevent ambiguity and complexity, such as the diamond problem. Instead, they use interfaces to allow a class to implement multiple behaviors without conflict.

🎯 Purpose & Use Case

  • βœ… Promotes cleaner and more maintainable class hierarchies
  • βœ… Interfaces allow multiple inheritance-like behavior
  • βœ… Prevents the diamond problem found in multiple base classes
  • βœ… Encourages better design patterns like composition over inheritance

πŸ’» Real Code Example

Multiple Interface Inheritance:

public interface IFly {
    void Fly();
}

public interface ISwim {
    void Swim();
}

public class Duck : IFly, ISwim {
    public void Fly() => Console.WriteLine("Flying...");
    public void Swim() => Console.WriteLine("Swimming...");
}

❓ Interview Q&A

Q1: Can a class inherit from multiple classes in .NET?
A: No, only single class inheritance is allowed.

Q2: What is supported for multiple inheritance in .NET?
A: Multiple interface inheritance.

Q3: What is the diamond problem?
A: Ambiguity caused when two base classes have the same method and are inherited by a child class.

Q4: How does .NET handle behavior reuse instead of multiple inheritance?
A: Through interfaces and composition.

Q5: Is interface implementation mandatory in C#?
A: Yes, all methods must be implemented unless it's a default interface method.

Q6: Can a class implement multiple interfaces with same method signature?
A: Yes, but the implementation will be shared unless explicitly defined.

Q7: Why is multiple class inheritance avoided in .NET?
A: To reduce complexity and avoid ambiguous calls.

Q8: What is composition over inheritance?
A: Using contained classes (objects) instead of inheritance to reuse behavior.

Q9: Are interfaces part of the .NET Type System?
A: Yes, they are reference types.

Q10: Can interfaces have default implementations in C# 8+?
A: Yes, via default interface methods.

πŸ“ MCQs

Q1. Can a class inherit from multiple classes in .NET?

  • Yes
  • No
  • Only in Blazor
  • Only with base keyword

Q2. What is allowed in .NET for multiple inheritance?

  • Multiple base classes
  • Multiple constructors
  • Multiple interface implementation
  • Multiple properties only

Q3. Why is multiple class inheritance avoided in .NET?

  • Performance issues
  • To avoid ambiguity and complexity
  • Security
  • Memory leaks

Q4. What is the diamond problem?

  • A memory error
  • Ambiguity from multiple inheritance
  • A compiler warning
  • A bug in CLR

Q5. Which version of C# introduced default interface methods?

  • C# 6.0
  • C# 7.1
  • C# 8.0
  • C# 5.0

Q6. What design principle prefers object composition?

  • Singleton
  • Factory
  • Composition over inheritance
  • Abstract inheritance

Q7. Can two interfaces have the same method name?

  • No
  • Yes
  • Only if abstract
  • Only in VB.NET

Q8. What must a class do when implementing an interface?

  • Inherit a base class
  • Use reflection
  • Implement all interface methods
  • Use IDisposable

Q9. What type are interfaces in .NET?

  • Value types
  • Reference types
  • Enums
  • Structs

Q10. Which of the following is an interface?

  • Object
  • Array
  • IDisposable
  • System.Threading

πŸ’‘ Bonus Insight

If you ever feel the need for multiple base classes, you might be violating SOLID principles. Try breaking functionality into smaller interfaces or classes and compose them together.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: