Difference Between Abstract Class and Interface in C#

πŸ’‘ Concept: Abstract Class vs Interface

Abstract classes and interfaces are used to define contracts in object-oriented programming. While both allow defining methods that derived classes must implement, they serve different purposes and follow distinct rules in C#.

πŸ“˜ Quick Intro

An abstract class allows partial implementation and can include fields and constructors. An interface is a pure contract that specifies what a class should do, but not how. Interfaces allow multiple inheritance, whereas classes support only single inheritance.

🧠 Analogy

Think of an abstract class as a partially built building with basic utilities like plumbing installed, while an interface is a checklist of items (doors, windows, rooms) that the builder must implement from scratch. Abstract classes help provide structure; interfaces help enforce rules.

πŸ”§ Technical Explanation

  • Abstract Class: Can have implementation, fields, and constructors.
  • Interface: Only declares members (until C# 8, where default implementations are allowed).
  • Inheritance: A class can inherit one abstract class but implement multiple interfaces.
  • Access Modifiers: Abstract class members can have access modifiers; interface members are public by default.
  • Polymorphism: Both support polymorphism, but interfaces offer greater flexibility for loose coupling.

🎯 Use Cases

  • βœ… Use interfaces to define contracts across unrelated classes.
  • βœ… Use abstract classes when you need a base class with default implementations.
  • βœ… Abstract classes are useful for shared functionality; interfaces for plug-and-play behavior.
  • βœ… Use interfaces with dependency injection and unit testing frameworks.

πŸ’» Code Example

// Abstract class example
public abstract class Vehicle {
    public abstract void Start();
    public void Stop() {
        Console.WriteLine(""Stopping..."");
    }
}

// Interface example
public interface IElectric {
    void Charge();
}

// Derived class implementing both
public class Tesla : Vehicle, IElectric {
    public override void Start() {
        Console.WriteLine(""Starting electric vehicle..."");
    }

    public void Charge() {
        Console.WriteLine(""Charging battery..."");
    }
}

❓ Interview Q&A

Q1: Can abstract classes have fields?
A: Yes, they can.

Q2: Can interfaces have fields?
A: No, interfaces cannot have fields.

Q3: Can interfaces define constructors?
A: No, constructors are not allowed in interfaces.

Q4: Can we create an object of an abstract class?
A: No, abstract classes cannot be instantiated.

Q5: Can an abstract class implement an interface?
A: Yes, and it may defer implementation to subclasses.

Q6: What is the default access modifier of interface members?
A: Public.

Q7: Can abstract classes include implemented methods?
A: Yes.

Q8: Can interfaces have default methods in modern C#?
A: Yes, from C# 8 onward.

Q9: Can a class extend an abstract class and implement interfaces?
A: Yes.

Q10: Can we override a non-abstract method from abstract class?
A: Only if it’s marked virtual.

πŸ“ MCQs

Q1. What is a key feature of abstract classes?

  • They can’t be inherited
  • Only for static members
  • They can have method implementations
  • None

Q2. What’s true about interfaces (C# < 8)?

  • Can have fields
  • No implementation allowed
  • Have constructors
  • Access modifiers supported

Q3. Which supports multiple inheritance?

  • Abstract classes
  • Interfaces
  • Static classes
  • Delegates

Q4. Can an abstract class have constructors?

  • No
  • Yes
  • Only private
  • Only static

Q5. Can interfaces contain properties?

  • No
  • Yes
  • Only static
  • Only inherited

Q6. Which is better for DI and mocking?

  • Abstract classes
  • Interfaces
  • Static classes
  • Records

Q7. Can abstract classes define private methods?

  • No
  • Yes
  • Only virtual
  • Only protected

Q8. What’s used for common behavior and partial implementation?

  • Interface
  • Record
  • Static class
  • Abstract class

Q9. What is required to override an abstract method?

  • Use partial
  • Use sealed
  • Must be in a derived class
  • Must be private

Q10. Interfaces members default to which access?

  • Private
  • Protected
  • Internal
  • Public

πŸ’‘ Bonus Insight

You can combine both: use abstract class for shared implementation and interfaces to define additional contracts, giving the best of both inheritance and flexibility.

πŸ“„ PDF Download

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

πŸ” Navigation

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

Tags: