What is interface

Interface in C# is basically a contract in which we declare only signature. The class which implemented this interface will define these signatures. Interface is also a way to achieve runtime polymorphism. We can add method, event, properties and indexers in interface

Syntax of Interface

We declare interface by using the keyword interface which is as follows:-

public interface interface_name { }

Example of Interface

Create an interface and add some signature of method

Interface Idef1

	public interface Idef1
{
    //singnature of methods

    int sum(int x, int y);
    int mul(int x, int y);
}

		

implementation.cs

Now create class which implements this interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//implement the interface in class
public class implementation: Idef1
{
    //define the methods in the class which declared in interface
    public int sum(int x, int y)
    {

        return x + y;
   
    }
    public int mul(int p, int q)
    {

        return p+q;
       }

}

_Default.aspx.cs

Now call this method on page load

		using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        implementation imp = new implementation();
        Response.Write("Output of sum="+imp.sum(4, 5));
        Response.Write("<br/> Output of mul="+imp.mul(7, 8));

    }
}

		Interface in C sharp

		Figure 1
		

More than one Interface having Same Method

OR

Runtime Polymorphism

Now let’s move to the more than one interface. We can implement more than one interface in one class. This is the way to achieve the multiple inheritance.

Create two interfaces and create same method in both and implement in same class.

Interface 1

public interface Idef1
{
    //singnature of methods

    int sum(int x, int y);


}
		

Interface 2

public interface Idef2
{

    int sum(int x, int y);

}

		

Interface Implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

//implement the interface in class
public class implementation: Idef1, Idef2
{
    //define the methods in the class which declared in Idef1 and Idef2
    public int sum(int x, int y)
    {

        return x + y;
   
    }
   

}

		

As you can see that we implemented two interfaces into one class. We give one definition to the method. We will not get any error as in interface we only declared the method. That is only signature. So we can give one common definition to all same method having same signature. When we create the object of implementation class we can easily call sum method without any error.

But if we want to implement both methods separately then we will use following way:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

//implement the interface in class
public class implementation: Idef1, Idef2
{
    //implementation of Idef1 method
     int Idef1.sum(int x, int y)
    {

        return x + y;
   
    }

     //implementation of Idef2 method
     int Idef2.sum(int x, int y)
     {

         return 2*(x + y);

     }
   

}
		

Calling of these two methods on PageLoad

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        implementation imp = new implementation();
        Idef1 inter1 = imp;
        Idef2 inter2 = imp;
        Response.Write("Idef1 method output:-" + inter1.sum(2, 4));
        Response.Write("<br/>Idef2 method output:-" + inter2.sum(2, 4));

    }
}

		

As you can see that we create reference variable of type idef1 and idef2 and instantiate it with implementation class. This is also a way to achieve runtime polymorphism as at runtime we decide which method will be executed.

The output of this code will be as follows:-

		Interface implementation in C sharp

		Figure 2
		

Email Address

For any query you can send mail at info@techaltum.com
Thanks