Sunday 30 June 2013

Interface in Detail with example of runtime polymorphism

Interface in Detail

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
{

    //here we define our signature

}

For Example:-

Create an interface and add some signature of method

public interface Idef1
{
    //singnature of methods

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

}


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;
   
    }

}

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

    }
}

Output:-



Figure 1

More than one Interface having Same Method

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.

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.

Output:-

The output of this code as follows:-



Figure 2

Hope you enjoyed the article. For any query you can mail me at info@techaltum.com



No comments:

Post a Comment