DEV Community

Minaz Inamdar
Minaz Inamdar

Posted on • Edited on

C# Interfaces

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

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)
  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)

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();
  }
}
Enter fullscreen mode Exit fullscreen mode

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?");
    }
}
}
Enter fullscreen mode Exit fullscreen mode

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();
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
pbouillon profile image
Pierre Bouillon

Hey, thanks for this article !

I just have a remark about this point:

Interfaces cannot have method implementation, only method declaration.

It is now possible for an interface to have a default implementation of a method. For example, this works and compile just fine:

public interface IDefaultMethod
{
    void SomeMethod() { Console.WriteLine("Default implementation"); }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
inamdarminaz profile image
Minaz Inamdar

Thanks Pierre for the correction 😊