Sunday, 30 November 2025

Abstract Class vs Interface

Abstract Class vs Interface — Comparison

Abstract Class vs Interface (in C#)

What they are

  • Abstract class: a special class (declared with the abstract keyword) that cannot be instantiated. It can include both abstract members (without implementation) and concrete members (with implementation).
  • Interface: a contract (declared with the interface keyword) describing a set of members (methods, properties, events, indexers) that implementing classes/structs must provide. It describes “what” must be done, not “how.”

Key Differences

AspectAbstract ClassInterface
Members Can have both abstract (no body) and concrete (with body) methods/properties, and even fields. :contentReference[oaicite:0]{index=0} Members are (mostly) just declarations (method/property signatures). In older versions, no implementation. Modern C# (from C# 8) allows default implementations and static members, but interfaces still cannot have instance fields. :contentReference[oaicite:1]{index=1}
Fields / State Can have fields (instance or static), so can hold state. :contentReference[oaicite:2]{index=2} Cannot have instance fields. Interfaces are not meant to hold state. :contentReference[oaicite:3]{index=3}
Access Modifiers Members can have access modifiers (public, protected, private, etc.). :contentReference[oaicite:4]{index=4} Members are implicitly public (in C#), and you normally cannot specify other access modifiers. :contentReference[oaicite:5]{index=5}
Constructors Can have constructors (to initialize state, etc.). :contentReference[oaicite:6]{index=6} Cannot have constructors because interfaces cannot be instantiated directly. :contentReference[oaicite:7]{index=7}
Inheritance / Implementation A class can inherit from only one abstract class (single inheritance). :contentReference[oaicite:8]{index=8} A class or struct can implement multiple interfaces (multiple-interface implementation). :contentReference[oaicite:9]{index=9}
Typical Use Case Useful when you want to share common code or behavior among related classes, or maintain common state. Good for “is-a” relationships. :contentReference[oaicite:10]{index=10} Useful when you just want to define a capability (a contract) that many unrelated classes can implement. Good for “can-do” or “has capability” relationship. :contentReference[oaicite:11]{index=11}

When to Use What

  • If you have a group of closely related classes that share common behaviour or state, and you want to avoid code duplication — use an abstract class.
  • If you want to define a contract (a set of capabilities) that many different, possibly unrelated classes should implement — use an interface.
  • Because interfaces support multiple implementation, they are more flexible when you need to combine many capabilities across different types.
  • Abstract classes are better when you want to provide default behavior or base implementations that subclasses can reuse or extend.

Example in C#

abstract class Animal {
    public string Name;             // field

    public abstract void MakeSound(); // abstract method

    public void Sleep() {             // concrete method
        Console.WriteLine("Sleeping...");
    }
}

interface IWalker {
    void Walk();  // method signature only
}

class Dog : Animal, IWalker {
    public override void MakeSound() {
        Console.WriteLine("Bark!");
    }

    public void Walk() {
        Console.WriteLine("Dog is walking...");
    }
}
  

In this example:
Animal is an abstract class — it provides shared code (the Sleep() method) and a field (Name).
IWalker is an interface — it defines a “capability” (walking).
Dog inherits from Animal and implements IWalker, so it gets the common behavior and commits to supporting walking.

No comments:

Post a Comment