Monday, 21 March 2016

Interface

What is Interface?

Multiple inheritance is not available through classes in .NET because of a *Diamond Problem. So to achieve multiple inheritance, .NET comes up with the concept of Interface.

Interface provides a way to achieve runtime polymorphism. By using this, we can invoke functions from different types through the same Interface reference, whereas using virtual functions we can invoke functions from different types in the same inheritance hierarchy through the same reference.

How to declare Interface?

Interface is declared using the interface keyword as we declare a class. The default accessibility of an interface is public. Following example explains that how to declare an interface:

public interface IMyInterface
    {
        //Interface members
        void MyMethod1();
        double MyMethod2();
    }

Examples:

public class MyTransaction : IMyTransactions
   {
      private string myCode;
      private string myDate;
      private double myAmount;
      public MyTransaction()
      {
         myCode = string.Empty;
         myDate = string.Empty;
         myAmount = 0.0;
      }

    public void MyMethod1()
     {
     }

    public double MyMethod1()
     {
            return 4.6;
     }

      public MyTransaction(string c, string d, double a)
      {
         myCode = c;
         myDate = d;
         myAmount = a;
      }
    
      public double ReturnAmount()
      {
         return myAmount;
      }
    
      public void DisplayingTransactions()
      {
         Console.WriteLine("Transaction: {0}", myCode);
         Console.WriteLine("Date: {0}", myDate);
         Console.WriteLine("Amount: {0}", ReturnAmount());
      }
   }
   class MyTester
   {
      static void Main(string[] args)
      {
         MyTransaction objMyTransaction1 = new MyTransaction("20", "13/03/2016", 25000.00);
         MyTransaction objMyTransaction2 = new MyTransaction("21", "12/03/2016", 30000.00);
         objMyTransaction1.DisplayingTransactions();
         objMyTransaction2.DisplayingTransactions();
         Console.ReadKey();
      }

   }

Following result I'll be produced, when the above code is compiled and executed:

Transaction: 20
Date: 13/03/2016
Amount: 25000.00
Transaction: 21
Date: 12/03/2016
Amount: 30000.00

*Diamond Problem
The term Diamond problem means that when a class inherit from more then one classes and each class have a method with same name and with same signatures. If we face this problem same in the interface we use the full qualified name for a member as explain in following piece of code:

using System;

namespace InterfaceDemo
{
 class Sample : IFirst, ISecond
    {
        public static void Main()
        {
            System.Console.WriteLine("Hello Interfaces");
            Sample objSample = new Sample();
            IFirst ObjIFirst = objSample;
            ObjIFirst.MyMethod();
            ISecond objISecond = objSample;
            objISecond.MyMethod();
        }

        void IFirst.MyMethod()
        {
            System.Console.WriteLine("From IFirst.MyMethod");
        }

        void ISecond.MyMethod()
        {
            System.Console.WriteLine("From ISecond.MyMethod");
        }

    }

    interface IFirst
    {
        void MyMethod();
    }

    interface ISecond
    {
        void MyMethod();
    }
}

More details on Interface with RealTime example.

Interfaces in C# offer several benefits that contribute to building flexible, maintainable, and scalable applications. Here are some key advantages of using interfaces along with a real-time code example to illustrate these benefits:

Benefits of Interfaces in C#

1. Encapsulation and Abstraction:

Interfaces allow you to define a contract without revealing the implementation details. This promotes encapsulation and abstraction, making it easier to understand and manage code.


2. Polymorphism:

Interfaces enable polymorphism, allowing different classes to be treated as instances of the same interface. This makes it easier to write flexible and reusable code.

3. Decoupling:

Interfaces help in decoupling code by separating the definition of operations from their implementation. This makes it easier to change or replace implementations without affecting other parts of the application.

4. Testability:

Interfaces make it easier to write unit tests by allowing you to mock dependencies. This is crucial for testing individual components in isolation.

5. Multiple Inheritance:

While C# doesn't support multiple inheritance for classes, interfaces can be implemented by a class along with other interfaces, providing a way to achieve multiple inheritance of behavior.

Real-Time Code Example: Payment Processing System


Let's consider a payment processing system where we need to process payments through different payment gateways (e.g., PayPal, Credit Card). We can define an interface for payment processing and implement it for different payment gateways.

Defining the Interface


csharp

public interface IPaymentProcessor
{
    void ProcessPayment(decimal amount);
}


Implementing the Interface for PayPal


csharp

public class PayPalProcessor : IPaymentProcessor
{
    public void ProcessPayment(decimal amount)
    {
        Console.WriteLine($"Processing payment of {amount} through PayPal.");
        // Implementation for PayPal payment processing
    }
}


Implementing the Interface for Credit Card


csharp

public class CreditCardProcessor : IPaymentProcessor
{
    public void ProcessPayment(decimal amount)
    {
        Console.WriteLine($"Processing payment of {amount} through Credit Card.");
        // Implementation for Credit Card payment processing
    }
}

Using the Interface


csharp

public class PaymentService
{
    private readonly IPaymentProcessor _paymentProcessor;

    public PaymentService(IPaymentProcessor paymentProcessor)
    {
        _paymentProcessor = paymentProcessor;
    }

    public void MakePayment(decimal amount)
    {
        _paymentProcessor.ProcessPayment(amount);
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        IPaymentProcessor paypalProcessor = new PayPalProcessor();
        PaymentService paymentService1 = new PaymentService(paypalProcessor);
        paymentService1.MakePayment(100.00m);  // Output: Processing payment of 100.00 through PayPal.

        IPaymentProcessor creditCardProcessor = new CreditCardProcessor();
        PaymentService paymentService2 = new PaymentService(creditCardProcessor);
        paymentService2.MakePayment(150.00m);  // Output: Processing payment of 150.00 through Credit Card.
    }
}


Explanation


1. Encapsulation and Abstraction:

The `IPaymentProcessor` interface defines the contract for payment processing without revealing the implementation details. This abstracts the specifics of payment processing from the rest of the application.

2. Polymorphism:

The `PaymentService` class uses the `IPaymentProcessor` interface, allowing it to work with any payment processor that implements the interface. This demonstrates polymorphism, as the same `PaymentService` can handle both `PayPalProcessor` and `CreditCardProcessor`.

3. Decoupling:

By relying on the `IPaymentProcessor` interface, the `PaymentService` class is decoupled from the specific implementations of payment processors. This makes it easy to add new payment processors or change existing ones without modifying the `PaymentService` class.

4. Testability:

The use of interfaces makes it easy to mock the `IPaymentProcessor` dependency in unit tests, allowing for isolated testing of the `PaymentService` class.

5. Multiple Inheritance:

Although not shown in this example, a class can implement multiple interfaces to combine different sets of functionalities, achieving a form of multiple inheritance.

Overall, interfaces are a powerful tool in C# that promote better design practices and make it easier to build robust, maintainable, and testable applications.



(Practice Makes a Man Perfect)

No comments: