Showing posts with label Interface. Show all posts
Showing posts with label Interface. Show all posts

Sunday, 30 November 2025

Abstract Class vs Interface

In C#, both abstract classes and interfaces handle abstraction differently: abstract classes share base code and state among related types, while interfaces define a contract of capabilities across unrelated types.

What Are Abstract Classes and Interfaces?

  • Abstract class: An uninstantiable class (using abstract) supporting both abstract and concrete members.
  • Interface: A contract (using interface) dictating required members without dictating internal implementation.

Abstract Class vs Interface: Key Differences

Aspect Abstract Class Interface
Members & State Abstract and concrete methods/properties; can hold instance or static fields. Signatures only (though C# 8+ allows default implementations); no instance fields.
Modifiers & Setup Supports standard access modifiers; can define constructors. Members are implicitly public; cannot have constructors.
Inheritance Single class inheritance only. Multiple interface implementation supported.
Use Case "Is-a" relationship for shared base logic. "Can-do" relationship for cross-cutting capabilities.

When to Use Abstract Class vs Interface

  • Abstract class: Best for closely related classes sharing base behavior or state to eliminate duplicate code.
  • Interface: Best for defining flexible contracts across disparate, unrelated types.

C# Code Example: Using Both Together

abstract class Animal
{
    public string Name;
    public abstract void MakeSound();
    public void Sleep() { Console.WriteLine("Sleeping..."); }
}

interface IWalker
{
    void Walk();
}

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

How This Example Works

  • Animal provides shared state and concrete logic (Sleep()).
  • IWalker enforces the walking contract.
  • Dog combines both through single class inheritance and interface implementation.

What Is an Interface?

What Is an Interface?

What Is an Interface in C#?

An interface in C# is a contract that defines a set of methods, properties, events, or indexers—without providing any implementation. A class or struct that implements an interface must provide the implementation for all its members.

Key Features of Interfaces

  • Cannot contain implementation (except default interface methods in newer C# versions).
  • A class can implement multiple interfaces (supports multiple inheritance).
  • Defines “what” a class should do, not “how” it should do it.
  • Members are public by default.

Example in C#

interface IAnimal
{
    void MakeSound();   // method without implementation
    void Sleep();       // another method
}

class Dog : IAnimal
{
    public void MakeSound()
    {
        Console.WriteLine("Bark!");
    }

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

Here, IAnimal is an interface that declares two methods. The Dog class implements this interface and provides the actual method bodies.