C# Interview Question #20

What is an abstract class, and how is it different from an interface?

OOP & Core C# Concepts Junior Beginner

Quick Interview Answer

An abstract class is a class that cannot be instantiated directly and is intended to act as a base class. It can contain abstract members as well as fully implemented members, fields, properties, constructors, and protected state.

Detailed Explanation

An abstract class is a class that cannot be instantiated directly and is intended to act as a base class. It can contain abstract members as well as fully implemented members, fields, properties, constructors, and protected state.

An interface primarily represents a contract or capability. A class can implement multiple interfaces but can inherit from only one class.

Use an interface when unrelated components should follow the same contract or when loose coupling is the main goal. Use an abstract class when closely related derived classes need shared implementation or shared state.

Questions 1–10: C# and .NET Fundamentals

Questions 11–20: Object-Oriented Programming

Code Example

public abstract class PaymentService
{
    public void LogPayment()
    {
        Console.WriteLine("Payment logged.");
    }

    public abstract Task<bool> PayAsync(decimal amount);
}

public class CardPaymentService : PaymentService
{
    public override Task<bool> PayAsync(decimal amount)
        => Task.FromResult(true);
}