What is an Abstract Class in C#?

πŸ’‘ Concept: Abstract Class

An abstract class in C# is a class that cannot be instantiated directly. It provides a base structure for derived classes and may contain abstract members (no implementation) as well as concrete members (with implementation).

πŸ“˜ Quick Intro

Abstract classes allow developers to define reusable logic while enforcing certain methods that must be implemented by subclasses. They help maintain a shared contract with partial implementations.

🧠 Analogy

Think of an abstract class as a partially assembled car blueprint. It has wheels and a frame ready, but each manufacturer (subclass) must install their own engine or interiors to complete it. You can’t drive the blueprintβ€”you need the final model.

πŸ”§ Technical Explanation

  • Declared with the abstract keyword.
  • May contain abstract methods (no body) and concrete methods (with implementation).
  • Cannot be instantiated directly; must be inherited.
  • Useful for creating a base class with default logic and required customization.
  • Can define constructors and fields unlike interfaces.

🎯 Use Cases

  • Defining base classes with shared logic across multiple subclasses.
  • Providing a skeletal implementation that enforces certain behaviors in child classes.
  • Combining common method logic and customizable operations via abstraction.

πŸ’» Real Code Example

public abstract class Animal {
    public abstract void Speak(); // abstract method
    public void Sleep() {
        Console.WriteLine("Sleeping...");
    }
}

public class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("Woof!");
    }
}

// Usage
Animal dog = new Dog();
dog.Speak();  // Output: Woof!
dog.Sleep();  // Output: Sleeping...

❓ Interview Q&A

Q1: What is an abstract class?
A: A class that cannot be instantiated and may contain abstract and concrete members.

Q2: Can abstract classes have constructors?
A: Yes, and they are called when a derived class is instantiated.

Q3: Can abstract classes implement interfaces?
A: Yes, and they can partially implement them.

Q4: Can an abstract class have fields?
A: Yes, unlike interfaces.

Q5: Can we create an object of an abstract class?
A: No, they must be inherited to use.

Q6: Can an abstract class be sealed?
A: No, sealed and abstract are mutually exclusive.

Q7: What happens if a derived class doesn't implement all abstract members?
A: It must be marked abstract itself.

Q8: Can abstract classes have static methods?
A: Yes, like any other class.

Q9: Why use an abstract class over interface?
A: When you want to include shared logic with enforcement.

Q10: Can an abstract class inherit another class?
A: Yes, and it can also be part of a hierarchy.

πŸ“ MCQs

Q1. What keyword defines an abstract class?

  • static
  • sealed
  • abstract
  • virtual

Q2. Can an abstract class be instantiated?

  • Yes
  • No
  • Only with constructor
  • Using new keyword

Q3. Can abstract class have constructors?

  • No
  • Yes
  • Only if static
  • Only if sealed

Q4. Which can abstract classes contain?

  • Only abstract methods
  • Only fields
  • Both abstract and concrete methods
  • Only static members

Q5. Can abstract classes have access modifiers?

  • No
  • Only public
  • Yes
  • Only internal

Q6. If a subclass doesn't override an abstract method, it must be:

  • Sealed
  • Final
  • Static
  • Abstract

Q7. Which scenario best suits abstract classes?

  • Multiple inheritance
  • Partial implementation with enforced behavior
  • Global constants
  • Private inheritance

Q8. Are abstract classes used in polymorphism?

  • No
  • Yes
  • Sometimes
  • Only if sealed

Q9. What must derived classes do with abstract methods?

  • Ignore them
  • Copy them
  • Override them
  • Rename them

Q10. Can you mark an abstract method as static?

  • Yes
  • No
  • Only in C# 9
  • With virtual keyword

πŸ’‘ Bonus Insight

Abstract classes work well in frameworks where shared behaviors are centralized and enforced across multiple components. They are often used in base controller classes, repositories, or core engine logic where consistency is critical.

πŸ“„ PDF Download

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

πŸ” Navigation

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

Tags: