C# Interfaces
Tags: csharp, beginners, interface
Published: 2022-10-26 18:49:08

## Abstraction
What does abstraction mean? In the simplest terms, it means hiding the convoluted implementations and exposing the crucial parts. But is that it?

Well yeah, that's it. Thanks for reading.


![CoderMan](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28zw4koc37g5d2m4bqpi.png)

Object-oriented programming takes inspiration from the real world. The way I think of it is by looking around. Look around you, and come up with three things that you know how to use but do not know how it works internally.
I can name a few, a laptop, a smartphone, and a game controller.

---

### Interface

Interface is one way of achieving abstraction, the other being abstract class.

> Interfaces will only show the capabilities of a class. 

1. Interfaces have only method declarations, not method implementation 
(Update : they can now have default implementation read more here [Default Interface Method](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/default-interface-methods-versions))
2. The methods within the interface are always public by default.
3. They cannot have fields, but they can have properties. 
4. Class implementing the interface must provide a definition for the interface's method with the same method signature.
5. A single class can implement multiple interfaces. But extending multiple base classes are not allowed ([The Diamond Problem](https://www.programmingwithshri.com/2018/09/what-is-diamond-problem-in-c.html))



####Interface Chaining
When an Interface2 is implementing another Interface1, and class Dummy is implementing Interface2, class Dummy must provide implementation for all the members of both the interfaces.

https://replit.com/@inamdarminaz/InterfaceChaning 

```
using System;
namespace InterfaceChaining{

  public interface Interface1
  {
      void Print1();
  }

  public interface Interface2 : Interface1 //chaining inheritance
  {
      void Print2();
  }

public class Dummy : Interface2
{
    // Implement Interface1 and Interface2 all members because Interface2 has chained inheritance from interface1
    public void Print1()
    {
        Console.WriteLine("Interface1.Print1();");
    }

    public void Print2()
    {
         Console.WriteLine("Interface2.Print2();");
    }
}
}


class Program {
  public static void Main (string[] args) {
    InterfaceChaining.Dummy dummy = new InterfaceChaining.Dummy();
    dummy.Print1();
    dummy.Print2();

   // We cannot create instance of an interface.
    //But an interface  reference variable can point to derieved class object.
    InterfaceChaining.Interface1 interface1 = new InterfaceChaining.Dummy();
    interface1.Print1();
  }
}
```


#### Explicit Interface

Consider two interfaces (Interface1 and Interface2)  have same method name and signature. If these two interfaces are implemented by a single class then which method will it point to? Interface1's Print() or Interface2's Print()? 

To avoid this confusion, developers use explicit interfaces which will specify which interface's method will be called.

Let's understand the ambiguity from below code
```
using System;
namespace ExplicitInterface{

  public interface Interface1
  {
      void Print();
  }

  public interface Interface2
  {
      void Print();
  }

public class Dummy : Interface1, Interface2
{
//Since the below method is implementing both the interfaces
//there won't be an error here. But when this function is called
//from main() which interface method will it point to?

// To avoid abiguity we need to implement Explicit Interface
    public void Print()
    {
        Console.WriteLine("Which interface am I pointing to?");
    }
}
}
```

This could solved by using explicit interfaces, by following these steps.


1. Get rid of access modifiers.
2. Set the method as "ReturnType InterfaceName.MethodName()"
3. Access the methods using interface refrence variables.
https://replit.com/@inamdarminaz/ExplicitInterfaces

```

public class Dummy : Interface1, Interface2
{
    void Interface1.Print()
    {
        Console.WriteLine("Interface1.Print();");
    }
    void Interface2.Print()
    {
        Console.WriteLine("Interface2.Print();");
    }
}
}


class Program {
  public static void Main (string[] args) {
    //But an interface  reference variable can point to derieved class object.
    ExplicitInterface.Interface1 interface1 = new ExplicitInterface.Dummy();
    interface1.Print();
    ExplicitInterface.Interface2 interface2 = new ExplicitInterface.Dummy();
    interface2.Print();
  }
}
```

---