Friday 7 December 2012

Interface in c#


Interfaces in C# (For Beginners)

Introduction
Interfaces in C # provide a way to achieve runtime polymorphism. Using interfaces we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions from different classes in the same inheritance hierarchy through the same reference. Before things start getting difficult let me start using simple and short examples to explain the concept of interfaces. Here's a short example that shows you what an interface looks like.

P1.cs

 Collapse | Copy Code
class Demo
{
  public static void Main()
  {
    System.Console.WriteLine("Hello Interfaces");
  }
}
 
interface abc
}

Output

 Collapse | Copy Code
Hello Interfaces
The above program compiles and runs successfully to produce the desired output. The above program consists of a classDemo and within it an entry point function Main() that prints Hello Interfaces. The above program also defines an interface abc. Interface abc is empty at this point of time. Let's add some elements to this interface.

P2.cs

 Collapse | Copy Code
class Demo
{
  public static void Main()
  {
    System.Console.WriteLine("Hello Interfaces");
  }
}
 
interface abc
{
  int x;  
}

Output

 Collapse | Copy Code
P2.cs(11,3): error CS0525: Interfaces cannot contain fields
Error! Interfaces in C# cannot contain fields i.e variables. The above program declared an integer variable x in the interface abc. And that's what hit the C# compiler badly.

P3.cs

 Collapse | Copy Code
class Demo
{
  public static void Main()
  {
    System.Console.WriteLine("Hello Interfaces");
  }
}
 
interface abc
{
  void xyz()
  {
     System.Console.WriteLine("In xyz");
  }  
}

Output

 Collapse | Copy Code
P3.cs(11,8): error CS0531: 'abc.xyz()': interface members cannot have a 
    definition
This time over we included a function xyz() inside the interface found that this too hurt the C# compiler. It told us loudly that interface members cannot have a defination. Does this mean that if we just have a function declaration inside the interface abc that is fine with the C# compiler? Let's find it out.

P4.cs

 Collapse | Copy Code
class Demo
{
  public static void Main()
  {
    System.Console.WriteLine("Hello Interfaces");
  }
}
 
interface abc
{
  void xyz();
}

Output

 Collapse | Copy Code
Hello Interfaces
The above program compiles and runs successfully to produce the desired output. Finally we made the compiler happy. Interfaces in C# can only contain function declarations. Now let us see interfaces in action.
Interfaces are contracts that a class implements in its own way. This means an interface will contain function prototypes and a class that marries this interface will have to take the responsibility of defining the functions whose prototypes are declared by the marrying interface.
So its time to perform the marriage between our groom class Demo and the bride interface abc.

P4.cs

 Collapse | Copy Code
class Demo : abc
{
  public static void Main()
  {
    System.Console.WriteLine("Hello Interfaces");
  }
}
 
interface abc
{
  void xyz();
}

Output

 Collapse | Copy Code
P4.cs(1,7): error CS0535: 'Demo' does not implement interface member 
    'abc.xyz()'
P4.cs(11,8): (Location of symbol related to previous error)
Well, in the above program class Demo did marry the interface abc through the line class demo : abc but as usual there's a small misunderstanding between the newlyweds. Class Demo needs to take the responsibility of defining the functions whose prototypes are declared by the marrying interface abc. Since class Demo in the above program has not been implemented i.e. defined the function xyz whose prototype is declared by the marrying interface abc we get an error in the above program. To fix this issue, the class Demo has to take the responsiility of defining the function xyzwhose prototype is declared by the marrying interface abc. And that is what you get to see in the following program.

P5.cs

 Collapse | Copy Code
class Demo : abc
{
  public static void Main()
  {
    System.Console.WriteLine("Hello Interfaces");
  }
 
  void xyz()
  {
     System.Console.WriteLine("In xyz");
  }  
}
 
interface abc
{
  void xyz();
}

Output

 Collapse | Copy Code
a.cs(1,7): error CS0536: 'Demo' does not implement interface member 
    'abc.xyz()'.'Demo.xyz()' is either static, not public, 
    or has  the wrong return type.
a.cs(16,8): (Location of symbol related to previous error)
a.cs(7,8): (Location of symbol related to previous error)
Error again! It's not enough for the class Demo to implement the function xyz. It has to impress the bride interface abc by declaring its implementation of xyz as public. And that's what is done by the following program.

P6.cs

 Collapse | Copy Code
class Demo : abc
{
  public static void Main()
  {
    System.Console.WriteLine("Hello Interfaces");
    xyz();
  }
 
  public void xyz()
  {
     System.Console.WriteLine("In xyz");
  }  
}
 
interface abc
{
  void xyz();
}

Output

 Collapse | Copy Code
Hello Interfaces
In xyz
Bingo! The above program compiles and runs successfully to produce the desired output. As mentioned earlier using interfaces we can invoke functions from different classes using the same interface reference. For this, we need to have different classes to implement the same interface. In the above program our class Demo is implementing the interfaceabc. Let's have another class Sample that implements the interface abc.

P7.cs

 Collapse | Copy Code
class Demo : abc
{
  public static void Main()
  {
    System.Console.WriteLine("Hello Interfaces");
    Demo refDemo = new Demo();
    refDemo.xyz();
    Sample refSample = new Sample();
    refSample.xyz();    
  }
 
  public void xyz()
  {
     System.Console.WriteLine("In Demo :: xyz");
  }  
}
 
interface abc
{
  void xyz();
}
 
class Sample : abc
{
  public void xyz()
  {
     System.Console.WriteLine("In Sample :: xyz");
  }  
}

Output

 Collapse | Copy Code
In Demo :: xyz
In Sample :: xyz
The above program compiles and runs successfully to produce the desired output. refDemo is a reference to the object of class Demo. refSample is a reference to the object of class Sample. Both the classes implement the interface abcand hence define their own implementation of the function xyz(). From within the entry point function Main() xyz()of the respective classes Demo and Sample are invoked through references refDemo and refSample.
Now that we have two different classes implementing the same interface its time to show you how to invoke functions from different classes using the same interface reference.

P8.cs

 Collapse | Copy Code
class Demo : abc
{
  public static void Main()
  {
    System.Console.WriteLine("Hello Interfaces");
    abc refabc = new Demo();
    refabc.xyz();
    abc refabc = new Sample();
    refabc.xyz();    
  }
 
  public void xyz()
  {
     System.Console.WriteLine("In Demo :: xyz");
  }  
}
 
interface abc
{
  void xyz();
}
 
class Sample : abc
{
  public void xyz()
  {
     System.Console.WriteLine("In Sample :: xyz");
  }  
}

Output

 Collapse | Copy Code
In Demo :: xyz
In Sample :: xyz
The above program compiles and runs successfully to produce the desired output. Inside Main() we have an interface reference refabc of type interface abc. Reference of object of class Demo is stored in refabc and xyz() of classDemo is invoked using refabc. Next, the reference of object of class Sample is stored in refabc and xyz() of classSample is invoked using refabc. Thus, we were able to invoke xyz() that belongs to different classes Demo andSample via a common interface reference refabc.
The following program uses a for loop to invoke the functions of different classes Demo and Sample that implement the same interface "interface abc" using a single interface reference refabc whose type matches the interface "interface abc" which the classes impliment.

P9.cs

 Collapse | Copy Code
class Demo : abc
{
  public static void Main()
  {
    abc [] refabc = {new Demo(), new Sample()} ;
    for (int i = 0; i<= 1; i++)
      refabc[i].xyz();
  }
 
  public void xyz()
  {
     System.Console.WriteLine("In Demo :: xyz");
  }  
}
 
interface abc
{
  void xyz();
}
 
class Sample : abc
{
  public void xyz()
  {
     System.Console.WriteLine("In Sample :: xyz");
  }  
}

Output

 Collapse | Copy Code
In Demo :: xyz
In Sample :: xyz
The above program compiles and runs successfully to produce the desired output. refabc is an array of type interfaceabc. It stores the references to objects of classes Demo and Sample. In the for loop, using the array refabc, we are invoking the function xyz() of class Demo and Sample. A class can impliment as many interfaces as it wants. Take the following program.

P10.cs

 Collapse | Copy Code
class Demo : abc, def
{
  public static void Main()
  {
    System.Console.WriteLine("Hello Interfaces");
    abc refabc = new Demo();
    refabc.xyz();
  }
 
  public void xyz()
  {
     System.Console.WriteLine("In xyz");
  }  
 
  public void pqr()
  {
     System.Console.WriteLine("In xyz");
  }
}
 
interface abc
{
  void xyz();
}
 
interface def
{
  void pqr();
}

Output

 Collapse | Copy Code
Hello Interfaces
In xyz
The above program compiles and runs successfully to produce a desired output. Class Demo implements interface abcand thereby function xyz(). Class Demo also impliments interface def and thereby function pqr(). ref abc which is a variable of type Interface abc, refers to object of class Demo. Next xyz() of Demo is invoked via refabc as refabc is a variable of type Interface abc which contains the prototype for function xyz().

No comments:

Post a Comment