0
0
C Sharp (C#)programming~7 mins

Interface vs abstract class decision in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Interfaces and abstract classes help organize code by defining rules for other classes. Choosing between them helps keep your program clear and easy to change.

When you want to define a contract that many different classes can follow, even if they are unrelated.
When you want to share some common code and state among related classes but still force them to implement some methods.
When you need to support multiple inheritance of behavior in C# (since a class can implement many interfaces but inherit only one class).
When you want to ensure certain methods are implemented but also provide default behavior.
When you want to separate what a class can do (interface) from how it does it (abstract class).
Syntax
C Sharp (C#)
public interface IExample
{
    void DoWork();
}

public abstract class AbstractExample
{
    public abstract void DoWork();

    public void CommonMethod()
    {
        Console.WriteLine("This is shared behavior.");
    }
}

An interface only declares method signatures without any implementation.

An abstract class can have both abstract methods (without body) and concrete methods (with body).

Examples
Interfaces can be empty or have many methods. They only declare what methods must be implemented.
C Sharp (C#)
public interface IWorker
{
    void Work();
}

// Empty interface example
public interface IEmpty {}

// Interface with multiple methods
public interface IAdvancedWorker
{
    void Work();
    void Rest();
}
Abstract classes can have methods with code and methods without code. They can also have fields and constructors.
C Sharp (C#)
public abstract class WorkerBase
{
    public abstract void Work();

    public void Rest()
    {
        Console.WriteLine("Resting...");
    }
}

// Abstract class with only abstract methods
public abstract class PureAbstract
{
    public abstract void DoSomething();
}
Interfaces can have default method implementations starting from C# 8, but this is less common and should be used carefully.
C Sharp (C#)
// Edge case: Interface with default implementation (C# 8+)
public interface IDefaultWorker
{
    void Work();
    void Rest() => Console.WriteLine("Default rest");
}
Sample Program

This program shows a dog class that inherits an abstract class and implements an interface, and a cat class that only implements the interface. Dog can use shared code from the abstract class, but cat cannot.

C Sharp (C#)
using System;

public interface IAnimal
{
    void Speak();
}

public abstract class AnimalBase
{
    public abstract void Speak();

    public void Eat()
    {
        Console.WriteLine("Eating food.");
    }
}

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

public class Cat : IAnimal
{
    public void Speak()
    {
        Console.WriteLine("Meow!");
    }
}

class Program
{
    static void Main()
    {
        Dog dog = new Dog();
        Cat cat = new Cat();

        Console.WriteLine("Dog says:");
        dog.Speak();
        dog.Eat();

        Console.WriteLine("Cat says:");
        cat.Speak();
        // cat.Eat(); // Not available because Cat does not inherit AnimalBase
    }
}
OutputSuccess
Important Notes

Time complexity: Choosing interface or abstract class does not affect runtime performance directly.

Space complexity: Both add no extra memory overhead beyond normal class inheritance.

Common mistake: Trying to add fields or method implementations in interfaces before C# 8, which is not allowed.

Use interfaces when you want to define capabilities without implementation. Use abstract classes when you want to share code and force some methods to be implemented.

Summary

Interfaces define what methods a class must have but do not provide code.

Abstract classes can provide some shared code and force subclasses to implement others.

Choose interfaces for unrelated classes that share behavior, and abstract classes for related classes sharing code.