C# Abstract Class

An abstract class is a way to achieve abstraction in object-oriented programming (OOP). It allows you to hide complex implementation details and expose only the essential features that define an object’s behavior. By using abstraction, developers can focus on what an object does rather than how it performs its tasks. In C#, abstraction is primarily implemented through abstract classes and interfaces.

What is an Abstract Class in C#?

An Abstract Class in C# is a class declared using the abstract keyword. It serves as a foundational blueprint that other classes can inherit from, but it cannot be instantiated directly.

An abstract class can contain both abstract methods (methods without implementation) and non-abstract methods (methods with implementation). This flexibility allows developers to define a common structure or behavior for all derived classes, while still giving each subclass the freedom to implement specific functionalities as needed.

In essence, an abstract class acts as a base class that enforces a consistent design pattern across related classes in an application.

Syntax:


using System;

public abstract class BaseClass
{
    // Abstract method - must be implemented in the derived class
    public abstract void DisplayInfo();

    // Non-abstract method - has a ready implementation
    public void Log(string message)
    {
        Console.WriteLine($"Log: {message}");
    }
}

public class ChildClass : BaseClass
{
    // Implementing the abstract method
    public override void DisplayInfo()
    {
        Console.WriteLine("Displaying information from ChildClass.");
    }
}

class Program
{
    static void Main()
    {
        // Creating object of the child class using base class reference
        BaseClass obj = new ChildClass();

        // Calling methods
        obj.DisplayInfo();
        obj.Log("Hello from Main method");
    }
}
Output:

Displaying information from ChildClass.
Log: Hello from Main method

Key Points About Abstract Classes in C#

  1. Declared using the abstract keyword: Abstract classes are defined using the abstract modifier, indicating that they serve as base classes and cannot be directly instantiated.
  2. Cannot be instantiated directly: You cannot create an object of an abstract class. Instead, it must be inherited by other classes that provide concrete implementations.
  3. Can contain abstract and non-abstract members: An abstract class may include abstract methods (without implementation) as well as fully implemented methods, giving flexibility to derived classes.
  4. Can include constructors, fields, and properties: Although abstract classes cannot be instantiated, they can still define constructors, fields, and properties used by child classes.
  5. Supports inheritance and polymorphism: Abstract classes enable polymorphic behavior by allowing derived classes to override base class members and provide specific implementations.
  6. Useful for shared logic across multiple classes: They are ideal when multiple subclasses share common behavior or logic but need unique implementations for certain methods.

By using abstract classes, developers can design cleaner, more maintainable, and extensible C# applications that align with real-world object-oriented principles.

Real-time Example:


public abstract class Employee
{
    public string Name { get; set; }
    public int EmployeeId { get; set; }

    public abstract decimal CalculateSalary();

    public void ShowDetails()
    {
        Console.WriteLine($"Employee: {Name}, ID: {EmployeeId}");
    }
}

public class FullTimeEmployee : Employee
{
    public decimal MonthlySalary { get; set; }

    public override decimal CalculateSalary()
    {
        return MonthlySalary;
    }
}

public class ContractEmployee : Employee
{
    public int HoursWorked { get; set; }
    public decimal HourlyRate { get; set; }

    public override decimal CalculateSalary()
    {
        return HoursWorked * HourlyRate;
    }
}

class Program
{
    static void Main()
    {
        Employee emp1 = new FullTimeEmployee { Name = "John", EmployeeId = 101, MonthlySalary = 60000 };
        Employee emp2 = new ContractEmployee { Name = "Amit", EmployeeId = 102, HoursWorked = 160, HourlyRate = 400 };

        emp1.ShowDetails();
        Console.WriteLine($"Monthly Salary: {emp1.CalculateSalary()}");

        emp2.ShowDetails();
        Console.WriteLine($"Monthly Salary: {emp2.CalculateSalary()}");
    }
}
Output:

Employee: John, ID: 101
Monthly Salary: 60000
Employee: Amit, ID: 102
Monthly Salary: 64000

Advantages of Using Abstract Classes in C#

  1. Promotes code reusability: Common logic and shared functionality can be defined once in the abstract base class, allowing all derived classes to reuse it efficiently.
  2. Encourages polymorphism: Abstract classes enable derived classes to override and customize behaviors, making the system more dynamic and easier to extend.
  3. Supports standardized architecture: By defining abstract methods, developers can enforce a consistent coding pattern and structure across large-scale or enterprise-level projects.

Limitations of Abstract Classes in C#

  1. Single inheritance limitation: A class can inherit from only one abstract class. This restriction can limit flexibility in cases where multiple base behaviors are required.
  2. Cannot be instantiated directly: Abstract classes cannot be used to create objects directly — they must be inherited by a derived class that provides concrete implementations.
  3. Interfaces may be better in some cases: If your goal is to define only method contracts without shared implementation, using an interface might be a simpler and more efficient choice.

Abstract classes are best suited for scenarios where you need both shared code and flexibility for future extension. However, if multiple inheritance or simple contracts are required, interfaces can often be the better option.


C# Abstract Class Interview Questions and Answers

1. What is an abstract class in C#?

An abstract class in C# is defined using the abstract keyword. It cannot be instantiated directly and may include both abstract methods (without implementation) and non-abstract methods (with implementation). Abstract classes act as base blueprints that other classes can extend.


2. Can an abstract class have constructors?

Yes. Abstract classes can contain constructors, which are executed when a derived class is instantiated. These constructors help initialize common fields or perform shared setup operations.


  • 3. Can we declare an abstract class without any abstract method?

    Yes. It’s valid to create an abstract class without abstract methods if your goal is to prevent direct instantiation while still sharing reusable code among derived classes.


    4. Can an abstract class implement an interface?

    Yes, an abstract class can implement one or more interfaces. It can either provide complete method implementations or leave them abstract for derived classes to define.


    5. What is the difference between an abstract class and an interface?

    Here’s a quick comparison between abstract classes and interfaces in C#:

    Feature Abstract Class Interface
    Implementation Can contain implemented methods and abstract methods Cannot contain implementations (except default methods in C# 8+)
    Fields Can define fields and properties Cannot define fields
    Multiple Inheritance Not supported Supported
    Constructors Allowed Not allowed

    6. Can we mark a static class as abstract?

    No. A static class cannot be declared as abstract because static classes cannot be inherited or instantiated.


    7. Can an abstract method be private?

    No, abstract methods cannot be private. They must be accessible to derived classes for implementation, and are typically declared as public or protected.


    8. What happens if a derived class doesn’t override all abstract methods?

    If a derived class does not implement all abstract methods from its base class, it must itself be declared as abstract. Otherwise, the compiler will generate an error.


    9. When should you use an abstract class?

    Use an abstract class when several related classes share common behavior or data but still need to provide their own specific implementations. It’s ideal for enforcing a consistent design across multiple modules.


    10. Give a few .NET framework examples of abstract classes.

    Common examples of abstract classes in the .NET Framework include:

    • Stream
    • DbCommand
    • DbConnection
    • XmlReader

    Conclusion

    An Abstract Class in C# serves as a foundation for creating structured, maintainable, and reusable code. It promotes polymorphism, ensures consistency across multiple derived classes, and supports clean architectural design — especially in enterprise-level applications.