Pages

Wednesday, May 15, 2013

interface in c#

->Interfaces is a group of member with empty bodies.
->Interfaces is like an abstract class but interfaces only unimpliment member( not contain any fields or implementations)
->Interfaces contain events,indexer,method and properties.
->within interface no access modifier is allowed.
->default access modifier is public
->Interface can not contain data member(instance & static field are not allowed)
->class & struct can impliment more than one interfaces.

Sample code:

public partial class interface2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        A10 a = new A10();   //mandotory to give impliment for all the member of interface in impliment class
        int r = a.add(10, 20);
        string r1 = a.sub(2, 4);
        int r2 = a.mul(2, 8);
        Response.Write(r + "<br/>" + r1 + "<br/>" + r2);
}
}
public interface I10   // I10 class cantain 3 method ,if u  not impliment one method it will be showing error
{
    int add(int x, int y);
    string sub(int x, int y);
    int mul(int x, int y);
}
public class A10 : I10
{
    public int add(int x, int y)
    {
        return x + y;
    }
    public string sub(int x, int y)
    {
        int z = x - y;
        return z.ToString();    //this line is convert string to int
    }
    public int mul(int x, int y)
    {
        return  x * y;
    }
}

Types in Interfaces:

Interface is two type 
1). explicity 
2).implicity
->we should be always access the explicity implimention interfaces member only by ussing interface variables , not by ussing object variable.
->always Better to use explicity implementation.
->C# not support multiple inheritance but support multiple interfaces implimentation

Sample code:(ex:imlpicity & explicity)

 public interface Icdc
  {
     int Add(int x,int y)
    int sub(int x,int y)
     }
public class B:Icdc        // implicity
{
   public int add(int x,int y)
   {
       return x+y;
   }
  public int sub (int x,int y)
   {
        return x-y;
    }
   public class C:Icdc      // Explicity
  {
      int Icdc.Add(int x,int y)
        {
         return x+y;
      }
       int Icdc.sub(int x,int y)
       {
           return x-y;
       } 
  }

Advantage:

Advantage1:  We need to declare an interface when we have series of classes which are not having common behavior, still if you want to mandate a method name along with signature and return type and if you want to leave the implementation to the developers of the class.

Advantage2:  Interfaces acts as contracts between two developers ( why or how this feature is helpful ?)
Explanation: Assume Developer D1 writing code in class A, this class A requires to call some of the methods present in Class B. Assume class B’s code has to be written by Developer D2. Assume some delay in writing the code happening from  Developer D2. In this situation Developer D1 ccanot be able to complete his code since class A’s code is depending on the Class B’s Code. This kind of dependencied between developers code we can avoid by using interfaces 

Q)When u declare an Interface?

Ans:when we have series of unrelated class.
explanation-
When your you have family of related classed which are sharing some common functionality better use Abstract class. When you have classes which are not related and still you may need same methods and properties then the choice is Interfaces.Also when you have code which changes freequently better to use Abstract class which provides versioning

No comments:

Post a Comment