MSDN Documentation

Understanding Interfaces and Abstract Classes in Object-Oriented Programming

In object-oriented programming (OOP), interfaces and abstract classes are powerful tools that help us achieve abstraction, enforce contracts, and promote code reusability and maintainability. While they share some similarities, they serve distinct purposes and have different characteristics.

What is an Interface?

An interface defines a contract. It specifies a set of method signatures (and sometimes properties, events, or indexers) that a class must implement if it claims to adhere to that interface. Interfaces do not provide any implementation for these members; they only declare what needs to be done.

Key characteristics of interfaces:

Example (Conceptual C#):


public interface IShape
{
    double GetArea();
    double GetPerimeter();
}

public class Circle : IShape
{
    public double Radius { get; set; }

    public Circle(double radius) { Radius = radius; }

    public double GetArea()
    {
        return Math.PI * Radius * Radius;
    }

    public double GetPerimeter()
    {
        return 2 * Math.PI * Radius;
    }
}

public class Square : IShape
{
    public double SideLength { get; set; }

    public Square(double sideLength) { SideLength = sideLength; }

    public double GetArea()
    {
        return SideLength * SideLength;
    }

    public double GetPerimeter()
    {
        return 4 * SideLength;
    }
}
        

What is an Abstract Class?

An abstract class is a class that cannot be instantiated directly. It can contain both abstract members (methods, properties, etc., that must be implemented by derived classes) and concrete members (methods with implemented logic that derived classes can inherit and use or override).

Key characteristics of abstract classes:

Example (Conceptual C#):


public abstract class Animal
{
    public string Name { get; set; }

    public Animal(string name) { Name = name; }

    // Abstract method - must be implemented by derived classes
    public abstract void MakeSound();

    // Concrete method - derived classes can use or override
    public void Eat()
    {
        Console.WriteLine($"{Name} is eating.");
    }
}

public class Dog : Animal
{
    public Dog(string name) : base(name) {}

    public override void MakeSound()
    {
        Console.WriteLine($"{Name} barks.");
    }
}

public class Cat : Animal
{
    public Cat(string name) : base(name) {}

    public override void MakeSound()
    {
        Console.WriteLine($"{Name} meows.");
    }
}
        

Key Differences Summarized

Implementation
Interface: No implementation (defines a contract).
Abstract Class: Can have partial or full implementation.
Inheritance
Interface: A class can implement multiple interfaces.
Abstract Class: A class can inherit from only one abstract class.
Members
Interface: Primarily method signatures, properties, etc. (implicitly public and abstract).
Abstract Class: Can have abstract and concrete members, constructors, fields, static members.
Purpose
Interface: Defines capabilities or roles a class can fulfill.
Abstract Class: Defines a common base for a group of related classes, often representing an "is-a" relationship.

When to Use Which?

By leveraging interfaces and abstract classes effectively, developers can build more robust, flexible, and maintainable software systems.