Abstract class vs interface in C# comparison showing differences and real examples

Abstract Class vs Interface in C#: Differences, When to Use & Real Examples

What is an Abstract Class in C#?

Key points:


Example: Vehicle Base

public abstract class Vehicle
{
    public int Speed { get; set; }

    // Concrete method - shared by all vehicles
    public void StartEngine()
    {
        Console.WriteLine("Engine started.");
    }

    // Abstract method - must be implemented by child classes
    public abstract void Drive();
}

What is an Interface in C#?

Example: Driveable Contract

public interface IDriveable
{
    void Drive();   // Declaration only, no code
    int Speed { get; set; }   // Property declaration
}
public class Car : IDriveable
{
    public int Speed { get; set; }

    public void Drive()
    {
        Console.WriteLine("Car is driving at " + Speed + " km/h");
    }
}

Difference Between Abstract Class and Interface in C#

When to Use Abstract Class vs Interface


Use Abstract Class when:

Use Interface when:

Simple Comparison:


Quick Tip:

Real-Time Example in C#


Example 1: Payment System

Abstract Class (Shared Logic)

public abstract class PaymentProcessor
{
    public void ValidatePayment()
    {
        Console.WriteLine("Validating payment...");
    }

    public abstract void ProcessPayment();
}

Interface (Specific Capability)

public interface ICreditCardPayment
{
    void PayWithCard(string cardNumber);
}

Implementation

public class CreditCardProcessor : PaymentProcessor, ICreditCardPayment
{
    public override void ProcessPayment()
    {
        Console.WriteLine("Processing credit card payment...");
    }

    public void PayWithCard(string cardNumber)
    {
        Console.WriteLine("Paid using card: " + cardNumber);
    }
}

Explanation:

Example 2: Notification System

Abstract Class

public abstract class Notification
{
    public string Message { get; set; }
    public string Recipient { get; set; }

    public abstract void Send();
}

Interface

public interface IEmailNotification
{
    void SendEmail();
}

Implementation

public class EmailNotification : Notification, IEmailNotification
{
    public override void Send()
    {
        Console.WriteLine("Sending notification to " + Recipient);
    }

    public void SendEmail()
    {
        Console.WriteLine("Email sent: " + Message);
    }
}

Explanation:

Example 3: Logging System

Abstract Class

public abstract class Logger
{
    public abstract void Log(string message);

    public void FormatMessage(string message)
    {
        Console.WriteLine("Formatted: " + message);
    }
}

Interface

public interface ILoggingService
{
    void LogToFile(string message);
}

Implementation

public class FileLogger : Logger, ILoggingService
{
    public override void Log(string message)
    {
        Console.WriteLine("Logging message...");
    }

    public void LogToFile(string message)
    {
        Console.WriteLine("Saved to file: " + message);
    }
}

Final Understanding:

Which is Faster – Abstract Class or Interface?

What does this mean in practice?

What should you focus on instead?

Why Do We Prefer Interfaces Over Abstract Classes?

Simple Explanation:

Example:

public interface IDriveable
{
    void Drive();
}

public interface IChargeable
{
    void Charge();
}

public class ElectricCar : IDriveable, IChargeable
{
    public void Drive()
    {
        Console.WriteLine("Car is driving...");
    }

    public void Charge()
    {
        Console.WriteLine("Car is charging...");
    }
}

What’s happening here?

FAQ (Frequently Asked Questions)

Q1: Can a class inherit multiple abstract classes in C#?

Q2: Can an interface have fields in C#?

Q3: Can we create an object of an abstract class?

Q4: Can a class implement multiple interfaces?

Q5: What is the main difference between abstract class and interface?

Conclusion

Related Articles

Leave a Comment

Your email address will not be published. Required fields are marked *