Implementing Interface Inheritance in C#

What is Interface Inheritance in C#?

Interface Inheritance in C# allows an interface to inherit from one or more other interfaces. This helps in designing modular and scalable applications by promoting reusability and flexibility.

Key Features of Interface Inheritance

  • An interface can inherit from multiple interfaces.
  • Derived interfaces inherit all methods of base interfaces.
  • Interfaces cannot inherit from classes.
  • All methods remain abstract, with no implementation.

Example: Inheriting Multiple Interfaces

The following example demonstrates how an interface can inherit from multiple interfaces.

Example: Interface Inheritance

interface IAnimal
{
    void Eat();
}

interface IPet
{
    void Play();
}

// Interface inheriting multiple interfaces
interface IDog : IAnimal, IPet
{
    void Bark();
}

// Implementing the interface
public class Dog : IDog
{
    public void Eat()
    {
        Console.WriteLine("Dog is eating.");
    }

    public void Play()
    {
        Console.WriteLine("Dog is playing.");
    }

    public void Bark()
    {
        Console.WriteLine("Dog barks!");
    }
}

// Usage
Dog myDog = new Dog();
myDog.Eat();  // Output: Dog is eating.
myDog.Play(); // Output: Dog is playing.
myDog.Bark(); // Output: Dog barks!
        

Here, IDog inherits from both IAnimal and IPet, and Dog provides implementations for all methods.

Interface Inheritance vs. Class Inheritance

While both interface and class inheritance allow reusability, they have key differences.

Feature Interface Inheritance Class Inheritance
Base Type Can inherit from multiple interfaces. Can inherit from only one base class.
Implementation Defines only method signatures; implementation is provided by classes. Base class can provide default implementations.
Fields & Constructors Cannot contain fields or constructors. Can contain fields, constructors, and methods.
Best Use Case When defining behavior contracts for multiple classes. When extending and reusing functionality in a class hierarchy.

Default Methods in Interface Inheritance (C# 8+)

Starting from C# 8.0, interfaces can contain default implementations.

Example: Default Method in Interface

interface IAnimal
{
    void Eat();

    // Default implementation
    void Sleep()
    {
        Console.WriteLine("Sleeping...");
    }
}

public class Cat : IAnimal
{
    public void Eat()
    {
        Console.WriteLine("Cat is eating.");
    }
}

// Usage
Cat myCat = new Cat();
myCat.Eat(); // Output: Cat is eating.

// myCat.Sleep(); // Error: Default methods require explicit interface reference

IAnimal animal = new Cat();
animal.Sleep(); // Output: Sleeping...
        

Here, the method Sleep() has a default implementation and must be accessed via an interface reference.

When to Use Interface Inheritance?

  • When multiple interfaces share common behaviors.
  • To allow classes to implement multiple behaviors while keeping code modular.
  • For designing reusable and loosely coupled components.
  • To enforce consistent behavior across multiple implementations.