Can a Class Implement Multiple Interfaces in C#?

πŸ’‘ Concept: Multiple Interface Implementation

Yes, a class in C# can implement multiple interfaces. This allows a class to inherit multiple behaviors without the limitations of single class inheritance.

πŸ“˜ Quick Intro

Multiple interface implementation is a core concept in C# object-oriented design. While a class can only inherit from one base class, it can implement several interfacesβ€”each defining a separate contract the class must fulfill.

🧠 Analogy

Imagine a person who is both a doctor and a writer. They follow the rules of the medical profession and writing guidelines. Similarly, a C# class can implement multiple interfaces, adhering to multiple role contracts.

πŸ”§ Technical Explanation

  • Use a comma-separated list of interface names after the class declaration.
  • All members from all interfaces must be implemented unless the class is abstract.
  • Explicit interface implementation can help resolve method name conflicts.
  • Each interface acts as a distinct contract; the class promises to provide implementations.
  • This feature supports multiple inheritance-like behavior in C#.

🎯 Use Cases

  • When a class needs to serve multiple roles (e.g., ILogger and IFormatter).
  • Building scalable and testable applications with dependency injection.
  • Unit testing via mockable interfaces.
  • Composing functionality without inheritance conflicts.

πŸ’» Real Code Example

public interface IReadable {
    void Read();
}

public interface IWritable {
    void Write();
}

public class Document : IReadable, IWritable {
    public void Read() {
        Console.WriteLine(""Reading document..."");
    }

    public void Write() {
        Console.WriteLine(""Writing document..."");
    }
}

❓ Interview Q&A

Q1: Can a class implement multiple interfaces?
A: Yes, it can implement any number of interfaces.

Q2: Why use multiple interfaces in a class?
A: To separate concerns and increase flexibility.

Q3: What happens if two interfaces have methods with the same name?
A: Use explicit implementation to resolve conflicts.

Q4: Can an interface extend multiple interfaces?
A: Yes, interfaces can inherit from multiple interfaces.

Q5: Does multiple interface implementation cause ambiguity?
A: Not if managed with proper method resolution.

Q6: Is multiple interface implementation runtime polymorphism?
A: Yes, it's a form of interface-based polymorphism.

Q7: Can interfaces contain fields?
A: No, interfaces only declare members.

Q8: Can interfaces define static methods?
A: Yes, starting from C# 8.

Q9: Is interface implementation required to be public?
A: Yes, unless explicitly implemented.

Q10: Can a struct implement interfaces?
A: Yes, structs can implement interfaces.

πŸ“ MCQs

Q1. What does a class use to implement multiple interfaces?

  • Inheritance
  • Generic types
  • Comma-separated list
  • Partial classes

Q2. Can a class inherit multiple interfaces in C#?

  • No
  • Yes
  • Only one
  • Only abstract interfaces

Q3. What is a benefit of implementing multiple interfaces?

  • More memory use
  • Faster compilation
  • Separation of concerns
  • Single responsibility

Q4. What feature allows different behaviors with same method name?

  • Overloading
  • Inheritance
  • Explicit implementation
  • Casting

Q5. Which type cannot implement multiple interfaces?

  • Class
  • Struct
  • Record
  • All can

Q6. Are method conflicts possible with multiple interfaces?

  • No
  • Yes
  • Only with generics
  • If sealed

Q7. Can interfaces be inherited by other interfaces?

  • No
  • Yes
  • Only one
  • Only internal ones

Q8. Which version introduced static members in interfaces?

  • C# 6
  • C# 7
  • C# 8
  • C# 9

Q9. How does C# handle interface member implementation?

  • Optional
  • Only override
  • Class must define all members
  • None of these

Q10. What keyword is used to implement interfaces?

  • ::
  • implements
  • : (colon)
  • new

πŸ’‘ Bonus Insight

Interfaces allow combining roles from different domains. This composition-based design is at the heart of clean, testable software in C# applications.

πŸ“„ PDF Download

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

πŸ” Navigation

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

Tags: