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

Explicit Interface Implementation

  • Producators
    Olumuyiwa Afolabi Category: C#
  • 3 months ago
  • 163
  • Back
Explicit Interface Implementation

Scenario: A Media Player Application

Imagine you're building a media player application that supports multiple types of media, such as audio and video. Each media type has its own specific methods, but they also share some common functionality. We'll use explicit interface implementation to handle this scenario.


Step 1: Define the Interfaces

We’ll create two interfaces:

  1. IPlayable â€“ For media that can be played.
  2. IStoppable â€“ For media that can be stopped.

Both interfaces have a method called Play() and Stop(), but their implementations will differ for audio and video.


public interface IPlayable
{
    void Play();
}

public interface IStoppable
{
    void Stop();
}


Step 2: Create the Media Classes

We’ll create two classes:

  1. AudioPlayer â€“ Implements both IPlayable and IStoppable.
  2. VideoPlayer â€“ Implements both IPlayable and IStoppable.

We’ll use explicit interface implementation to avoid naming conflicts and to provide specific behavior for each media type.


public class AudioPlayer : IPlayable, IStoppable
{
    // Implicit implementation of IPlayable.Play
    public void Play()
    {
        Console.WriteLine("Audio is playing...");
    }

    // Explicit implementation of IStoppable.Stop
    void IStoppable.Stop()
    {
        Console.WriteLine("Audio playback stopped.");
    }
}

public class VideoPlayer : IPlayable, IStoppable
{
    // Implicit implementation of IPlayable.Play
    public void Play()
    {
        Console.WriteLine("Video is playing...");
    }

    // Explicit implementation of IStoppable.Stop
    void IStoppable.Stop()
    {
        Console.WriteLine("Video playback stopped.");
    }
}


Step 3: Use the Media Player

Now, let’s use the AudioPlayer and VideoPlayer classes in a program. We’ll demonstrate how explicit interface implementation works.


class Program
{
    static void Main(string[] args)
    {
        // Create instances of AudioPlayer and VideoPlayer
        AudioPlayer audioPlayer = new AudioPlayer();
        VideoPlayer videoPlayer = new VideoPlayer();

        // Play audio and video
        audioPlayer.Play(); // Calls IPlayable.Play (implicit implementation)
        videoPlayer.Play(); // Calls IPlayable.Play (implicit implementation)

        // Stop audio and video (explicit implementation)
        ((IStoppable)audioPlayer).Stop(); // Calls IStoppable.Stop for audio
        ((IStoppable)videoPlayer).Stop(); // Calls IStoppable.Stop for video
    }
}


Step 4: Output

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

Copy

Audio is playing...
Video is playing...
Audio playback stopped.
Video playback stopped.


Step 5: Explanation

  1. Implicit Implementation:
  • The Play() method is implemented implicitly in both AudioPlayer and VideoPlayer. This means you can call Play() directly on the class instance.
  1. Explicit Implementation:
  • The Stop() method is implemented explicitly. This means you can only call Stop() when the object is cast to the IStoppable interface.
  • This is useful because it allows us to provide different behavior for Stop() in AudioPlayer and VideoPlayer without causing naming conflicts.

Step 6: Real-World Use Case

Imagine you’re building a plugin system for the media player. Some plugins might only support IPlayable, while others might support both IPlayable and IStoppable. Explicit interface implementation allows you to:

  • Hide the Stop() method from classes that don’t need it.
  • Provide specific behavior for Stop() in different media types.

Step 7: Complete Solution Code

Here’s the complete code for the solution:


using System;

public interface IPlayable
{
    void Play();
}

public interface IStoppable
{
    void Stop();
}

public class AudioPlayer : IPlayable, IStoppable
{
    // Implicit implementation of IPlayable.Play
    public void Play()
    {
        Console.WriteLine("Audio is playing...");
    }

    // Explicit implementation of IStoppable.Stop
    void IStoppable.Stop()
    {
        Console.WriteLine("Audio playback stopped.");
    }
}

public class VideoPlayer : IPlayable, IStoppable
{
    // Implicit implementation of IPlayable.Play
    public void Play()
    {
        Console.WriteLine("Video is playing...");
    }

    // Explicit implementation of IStoppable.Stop
    void IStoppable.Stop()
    {
        Console.WriteLine("Video playback stopped.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Create instances of AudioPlayer and VideoPlayer
        AudioPlayer audioPlayer = new AudioPlayer();
        VideoPlayer videoPlayer = new VideoPlayer();

        // Play audio and video
        audioPlayer.Play(); // Calls IPlayable.Play (implicit implementation)
        videoPlayer.Play(); // Calls IPlayable.Play (implicit implementation)

        // Stop audio and video (explicit implementation)
        ((IStoppable)audioPlayer).Stop(); // Calls IStoppable.Stop for audio
        ((IStoppable)videoPlayer).Stop(); // Calls IStoppable.Stop for video
    }
}


Step 8: Key Takeaways

  • Explicit interface implementation is useful when:
  • You have naming conflicts between interfaces.
  • You want to hide certain methods from the class API.
  • You need to provide different behavior for the same method in different contexts.
  • Implicit interface implementation is simpler and more intuitive for most use cases.


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