Interface in C#

Definition

An interface is a collection of data members and member functions, but it does not implement them. Interfaces are introduced to provide the feature of multiple inheritances to classes. Multiple inheritances are the feature of OOP which allows a class to inherit from multiple classes. The methods defined in an interface do not have an implementation, they only specify the parameters that they will take and the types of values they will return. An interface is always implemented in a class. The class that implements the interface needs to implement all the members of the interface.

In C#, interfaces are more flexible in their implementation, than in any other language. An interface in C# is the equivalent of an abstract base class. This implies that you cannot instantiate an object through an interface, but you can offer a set of functionality to classes that implement the interface. An interface can have the same access modifiers as a class, such as public and private. Let's learn about the following concepts in the context of declaring and implementing interfaces in C#.

  • Syntax of interfaces
  • Implementation of interfaces
  • Interfaces and inheritance

Syntax of interfaces

An interface is declared just like a class, but the only difference is that a class is declared using the class keyword, whereas, an interface is declared using the interface keyword. Interfaces behave as templates that have method declarations in them. These methods are abstract, as they do not have code in their body As discussed a class that implements an interface must implement all of its methods, otherwise, an error occurs You can declare an interface using the following syntax:

interface <interfaceName>
{
  //Abstract method Declaration in interface body
}

Consider the following code snippet that creates two interfaces named MyFirstInterface and MySecondInterface:

interface MyFirstInterface
{ 
  void MyFirstMethod();   //Abstract method in Interface Body
}
interface MySecondInterface
{
  void MySecondMethod();    //Abstract Method in Interface Body
}

In the preceding code snippet, both the MyFirstInterface and MySecondInterface interfaces have one abstract method named MyFirstMethod() and MySecondMethod(). Both methods are void and do not return any value.

Implementation of Interfaces

TO implement multiple inheritance in a program, you need to use interfaces. Implementing an interface is similar to inheriting a class. The following code snippet shows how to implement interfaces:

class ImplementInterface : MyFirstInterface, MySecondInterface
{
   static void Main()
   {
      ImplementInterface ABC=new ImplementInterface();
      //ABC object type casting in l1,l2 object
      MyFirstInterface l1=(MyFirstInterface)ABC;
      MySecondInterface l2=(MySecondInterface)ABC;
      l1.MyFirstMethod();
      l2.MySecondMethod();
    }
    public void MyFirstMehtod()
    {
       Console.writeLine("call MyFirstMethod() ");
    }
    public void MySecondMethod()
    {
       Console.writeLine("call MySecondMethod() ");
    }
 }

In the preceding code snippet, the ImplementInterface class and MySecondInterface interfaces. The MyFirstInterface interface contains a method MyFirstMethod() and the MySecondInterface interface contains a method MySecondMethod() both of which do not return any value. As seen in the preceding code snippet, we write the body of methods in the class that implements the interfaces.

Interfaces and Inheritance

When an interface is implemented by a base class, then the derived class of the base class automatically inherits the method of the interface. You can initialize an object of the interface by typecasting the object of the derived class with the interface itself. Consider the following code snippet to understand the concept of interface and inheritance more clearly:

interface MyInterface
{
 void Showdata();
}
class MyBaseClass:MyInterface   //Implements MyInterface
{
 public void ShowData()
 {
    //Method Body
 }
}
class MyDerivedClass:MyBaseClass   //Inherits MyBaseClass
{
 public new void ShowData()
 {
    //Method Body
 }
}
class MainClass
{
    public static void Main()
    {
        MyDerivedClass dc=new MyDerivedClass();
        dc.ShowData();
        MyInterface inter=(MyInterface) dc;
        inter.ShowData();
    }
}

In the preceding code snippet, an interface, MyInterface, is declared containing the ShowData() method. A base class, MyBaseClass, implements the MyInterface interface. Next, a derived class, MyDerivedClass, inherits the MyBaseClass class. Now, in the MainClass class an instance, dc, of the MyDerivedClass class is declared and initialized. Finally, the dc object is typecast to the inter-object of the MyInterface interface and used to call the ShowData() method.



Write a comment


Name Email Message