Proucators
  • Trending
  • Programming
    • C#
    • Java
    • Python
    • JavaScript
  • Cyber Security
    • Security Awareness
    • Network Security
    • Cloud Security
    • Data Protection
  • Databases
    • SQL Server
    • MongoDB
    • PostgreSQL
    • MySQL
    • Cassandra
    • Redis
    • Google Cloud SQL
    • Azure Cosmos DB
    • Apache Kafka
  • AI
    • Generative AI
    • Machine Learning
    • Natural Language Processing
    • Computer Vision
    • Robotics
  • Apps
    • Social Media
    • Productivity
    • Entertainment
    • Games
    • Education
    • Finance
    • Health and Fitness
    • Travel
    • Food Delivery
    • Shopping
    • Utilities
    • Business
    • Creativity
  • Tech News
    • Computing
    • Internet
    • IT
    • Cloud Service
Community
Accessdrive

Transforming digital capabilities through project-based training and expert offshore development services for web, mobile, and desktop applications.

  • Trending
  • Programming
    • C#
    • Java
    • Python
    • JavaScript
  • Cyber Security
    • Security Awareness
    • Network Security
    • Cloud Security
    • Data Protection
  • Databases
    • SQL Server
    • MongoDB
    • PostgreSQL
    • MySQL
    • Cassandra
    • Redis
    • Google Cloud SQL
    • Azure Cosmos DB
    • Apache Kafka
  • AI
    • Generative AI
    • Machine Learning
    • Natural Language Processing
    • Computer Vision
    • Robotics
  • Apps
    • Social Media
    • Productivity
    • Entertainment
    • Games
    • Education
    • Finance
    • Health and Fitness
    • Travel
    • Food Delivery
    • Shopping
    • Utilities
    • Business
    • Creativity
  • Tech News
    • Computing
    • Internet
    • IT
    • Cloud Service
Community
Find With Us
Producators

Interface Inheritance

  • Producators
    Olumuyiwa Afolabi Category: C#
  • 3 months ago
  • 146
  • Back
Interface Inheritance

1. What is Interface Inheritance?

Interface inheritance in C# allows one interface to inherit from another interface. This means the derived interface will include all the members (methods, properties, events) of the base interface, and it can also add new members. It’s like a child inheriting traits from a parent but also having its own unique traits.


2. Layman’s Explanation with Real-Life Example

Think of a vehicle hierarchy:

  • A Vehicle interface defines basic functionality like Start() and Stop().
  • A Car interface inherits from Vehicle and adds its own functionality like Accelerate().
  • A SportsCar interface inherits from Car and adds even more specific functionality like TurboBoost().

This way, you can build specialized interfaces without duplicating code.


3. Complete Solution: Real-World Project Example

Let’s build a vehicle management system using interface inheritance. We’ll define a base interface IVehicle and derive more specialized interfaces like ICar and ISportsCar.

Step 1: Define the Base Interface

The IVehicle interface defines basic functionality for all vehicles.


public interface IVehicle
{
    void Start();
    void Stop();
    string GetFuelType();
}


Step 2: Define Derived Interfaces

The ICar interface inherits from IVehicle and adds car-specific functionality.


public interface ICar : IVehicle
{
    void Accelerate();
    void Brake();
}

The ISportsCar interface inherits from ICar and adds sports car-specific functionality.


public interface ISportsCar : ICar
{
    void TurboBoost();
}

Step 3: Implement the Interfaces

Now, let’s create a class SportsCar that implements the ISportsCar interface.


public class SportsCar : ISportsCar
{
    public void Start()
    {
        Console.WriteLine("Sports car started.");
    }

    public void Stop()
    {
        Console.WriteLine("Sports car stopped.");
    }

    public string GetFuelType()
    {
        return "Premium Unleaded";
    }

    public void Accelerate()
    {
        Console.WriteLine("Sports car accelerating.");
    }

    public void Brake()
    {
        Console.WriteLine("Sports car braking.");
    }

    public void TurboBoost()
    {
        Console.WriteLine("Sports car turbo boost activated!");
    }
}


Step 4: Use the Interfaces in a Program

Let’s create a program to demonstrate how interface inheritance works.


class Program
{
    static void Main(string[] args)
    {
        // Create an instance of SportsCar
        SportsCar mySportsCar = new SportsCar();

        // Use IVehicle methods
        mySportsCar.Start();
        mySportsCar.Stop();
        Console.WriteLine($"Fuel type: {mySportsCar.GetFuelType()}");

        // Use ICar methods
        mySportsCar.Accelerate();
        mySportsCar.Brake();

        // Use ISportsCar methods
        mySportsCar.TurboBoost();
    }
}


Step 5: Output

When you run the program, you’ll see the following output:

Copy

Sports car started.
Sports car stopped.
Fuel type: Premium Unleaded
Sports car accelerating.
Sports car braking.
Sports car turbo boost activated!


Step 6: Explanation

  • Interface Inheritance:
  • ICar inherits from IVehicle, so it includes Start(), Stop(), and GetFuelType().
  • ISportsCar inherits from ICar, so it includes all methods from ICar and IVehicle, plus its own TurboBoost() method.
  • Implementation:
  • The SportsCar class implements ISportsCar, so it must provide implementations for all methods in IVehicle, ICar, and ISportsCar.

Step 7: Best Practices and Features

  • Best Practices:
  1. Use interface inheritance to avoid code duplication and create a clear hierarchy.
  2. Keep interfaces small and focused (Single Responsibility Principle).
  3. Use meaningful names for interfaces and methods.
  4. Avoid deep inheritance hierarchies (prefer composition over inheritance).
  5. Document the purpose of each interface clearly.
  • Features:
  • Allows you to build specialized interfaces without rewriting code.
  • Promotes loose coupling and modular design.
  • Enables polymorphism (e.g., treat SportsCar as IVehicle, ICar, or ISportsCar).

Step 8: Pros and Cons

  • Pros:
  • Code reusability: Derived interfaces inherit members from base interfaces.
  • Flexibility: You can create specialized interfaces without modifying existing ones.
  • Polymorphism: Objects can be treated as their base or derived interface types.
  • Cons:
  • Can lead to complex hierarchies if overused.
  • Deep inheritance chains can make the code harder to understand.

Step 9: Alternatives

  • Abstract Classes: Use abstract classes if you need to provide default implementations for some methods.
  • Composition: Instead of inheritance, use composition to combine functionality from multiple interfaces or classes.

Step 10: When to Use Interface Inheritance

  • Use interface inheritance when:
  • You need to create a hierarchy of related interfaces.
  • You want to enforce a contract across multiple levels of specialization.
  • You want to avoid duplicating code in related interfaces.


Step 11: Complete Solution Code

Here’s the complete code for the solution:


using System;

// Base interface
public interface IVehicle
{
    void Start();
    void Stop();
    string GetFuelType();
}

// Derived interface
public interface ICar : IVehicle
{
    void Accelerate();
    void Brake();
}

// Further derived interface
public interface ISportsCar : ICar
{
    void TurboBoost();
}

// Implementation
public class SportsCar : ISportsCar
{
    public void Start()
    {
        Console.WriteLine("Sports car started.");
    }

    public void Stop()
    {
        Console.WriteLine("Sports car stopped.");
    }

    public string GetFuelType()
    {
        return "Premium Unleaded";
    }

    public void Accelerate()
    {
        Console.WriteLine("Sports car accelerating.");
    }

    public void Brake()
    {
        Console.WriteLine("Sports car braking.");
    }

    public void TurboBoost()
    {
        Console.WriteLine("Sports car turbo boost activated!");
    }
}

// Program
class Program
{
    static void Main(string[] args)
    {
        // Create an instance of SportsCar
        SportsCar mySportsCar = new SportsCar();

        // Use IVehicle methods
        mySportsCar.Start();
        mySportsCar.Stop();
        Console.WriteLine($"Fuel type: {mySportsCar.GetFuelType()}");

        // Use ICar methods
        mySportsCar.Accelerate();
        mySportsCar.Brake();

        // Use ISportsCar methods
        mySportsCar.TurboBoost();
    }
}


Producators

Similar Post

Top 20 NuGet Packages You Must Add to Your .NET Application
Top 20 NuGet Packages You Must Add to Your .NET Application
Read Article
How to Build a Sentiment Analysis Tool Using C#
How to Build a Sentiment Analysis Tool Using C#
Read Article
Creating a Chatbot with C# and Microsoft Bot Framework
Creating a Chatbot with C# and Microsoft Bot Framework
Read Article
Image Classification Using C# and TensorFlow: A Step-by-Step Guide
Image Classification Using C# and TensorFlow: A Step-by-Step Guide
Read Article
Working with Predictive Maintenance Using C# and Azure Machine Learning
Working with Predictive Maintenance Using C# and Azure Machine Learning
Read Article
Natural Language Processing (NLP) in C#: A Beginner's Guide
Natural Language Processing (NLP) in C#: A Beginner's Guide
Read Article
Deep Learning with C#: Convolutional Neural Networks (CNNs)
Deep Learning with C#: Convolutional Neural Networks (CNNs)
Read Article

©2025 Producators. All Rights Reserved

  • Contact Us
  • Terms of service
  • Privacy policy