Wednesday, 30 October 2024

Abstract Class vs Interface in C# .NET

In C#, both abstract classes and interfaces are used to define abstract types that specify members without providing a full implementation. However, they serve different purposes and have distinct characteristics.

Here’s a comparison of abstract classes and interfaces:

1. Definition and Purpose

  • Abstract Class:

    • Serves as a base class that can provide default behavior (method implementations).
    • Can contain a mix of fully implemented methods, properties, fields, and abstract members that must be implemented by derived classes.
    • Used when classes share a common base but also need to be extended with additional features or behaviors.
  • Interface:

    • Defines a contract (only method, property, event, and indexer signatures) that implementing classes must fulfill.
    • Does not contain implementation (unless using C# 8.0 or later, where default interface methods are allowed).
    • Used to define capabilities or behaviors that can be added to different types, enabling polymorphism across unrelated classes.

2. Inheritance

  • Abstract Class:

    • Supports single inheritance (a class can inherit from only one abstract or non-abstract class).
    • Can inherit from another class and implement multiple interfaces.
  • Interface:

    • Supports multiple inheritance (a class can implement multiple interfaces).
    • Allows a class to "inherit" or implement multiple sets of functionality.

3. Member Types

  • Abstract Class:

    • Can have constructors, fields, properties, methods, events, and even static members.
    • Can contain both abstract (unimplemented) members and fully implemented members.
  • Interface:

    • Cannot contain constructors, fields, or static members.
    • Only contains member signatures (method signatures, property signatures, etc.).
    • From C# 8.0, interfaces can have default implementations, but they still cannot contain instance fields or constructors.

4. Access Modifiers

  • Abstract Class:

    • Members can have different access modifiers (public, protected, internal, etc.).
    • Allows greater control over the visibility of members.
  • Interface:

    • All members are public by default, as interfaces are meant to define a public contract.
    • Members cannot have access modifiers (except default interface methods in C# 8.0 or later).

5. When to Use

  • Abstract Class:

    • When you want to share common functionality among derived classes.
    • When classes have a hierarchical relationship and share a common base but need specific implementations or overrides.
  • Interface:

    • When you want to define a capability or behavior that multiple, unrelated classes should implement (e.g., IDisposable, IComparable).
    • When you want to achieve polymorphism without a common ancestor in the hierarchy.

6. Example of Abstract Class

public abstract class Animal { public abstract void Speak(); // Abstract method, must be implemented in derived classes public void Sleep() { Console.WriteLine("Animal is sleeping"); // Non-abstract method with implementation } } public class Dog : Animal { public override void Speak() { Console.WriteLine("Dog barks"); } }

7. Example of Interface

public interface IMovable { void Move(); } public interface IFlyable { void Fly(); } public class Bird : IMovable, IFlyable { public void Move() { Console.WriteLine("Bird is moving"); } public void Fly() { Console.WriteLine("Bird is flying"); } }

Summary Table

FeatureAbstract ClassInterface
Can have implementationsYesNo (except C# 8.0+ default interface methods)
InheritanceSingleMultiple
ConstructorsYesNo
FieldsYesNo
Access ModifiersYesNo (members are public by default)
Use CaseCommon base behavior and hierarchyDefining capabilities across unrelated classes

When to Use Which

  • Use abstract classes when there is a clear hierarchy, shared base behavior, or the need for partial implementations.
  • Use interfaces when you need a flexible way to define common behavior across various classes without establishing a strict inheritance hierarchy.
Share:

0 comments:

Post a Comment