Interfaces in C#

Background

In this article we will learn about one of the most reusable object oriented features of C#, Interfaces. We will learn about Interfaces from the basics because I have written this article focusing on the students and the beginners. Before proceeding further, please refer to my previous articles for a better understanding.

So, let us start from the basics of the Interfaces .

Interfaces

To overcome the absence of multiple inheritance in C# and allow a loose coupling of classes as well as to provide the some extensibility to existing classes, Interfaces are the best option in C# to do these things.

Some key points

  • Interfaces are declared using the interface keyword.
  • Default access modifiers for interface are internal.
  • Members of interfaces cannot be private.
  • We cannot be applied any access modifier to the interface members because by default they are public.
  • A class can implement several interfaces.
  • Interfaces must be implemented using classes.
  • An Interface does not have a constructor.
  • All methods inside the interfaces must be implemented.
  • Interfaces cannot contain a body of any of its methods.

Syntax

Interfaces are declared using the interface keyword as in the following:
 
interface [Interface Name]

Example

  1. public interface IA //ineterface
  2.  {  
  3.     string GetName(string a);  // Interfaces cannot contain body of its method
  4.          
  5.  }  
  6.   
  7.  public class ICar:IA //implementatin --interface implemented by using class ICar
  8.  {  
  9.      public string GetName(string a)  
  10.      {  
  11.          return a;  
  12.      }  
  13.  } 
In the preceding example, we have declared that the interface "IA" contains a "GetName" method that has no Implementation and then we have implemented an interface "IA" in the class ICar.
 
Its also possible that we can inherit two interfaces as a class as in the following:
Interface Inheritance 

Interviewers often ask in interviews how to inherit two interfaces, in other words can an interface be inherited in another interface. The answer is yes, let us see an actual use of that.
 
Example
  1. public interface BankAddress //Interface 1  
  2.    {  
  3.   
  4.        string Address(string city);     
  5.    }  
  6.   
  7.    public interface CustomerDeteails : BankAddress //Interface BankAddress inherited  
  8.    {  
  9.        int AccountDet(int AccNo);  
  10.          
  11.    }  
  12.    public class AccountInfo : CustomerDeteails ////Interface CustomerDeteails Implemented  
  13.    {  
  14.   
  15.        public int AccountDet(int AccNo)  //Inetrface CustomerDeteails Method  
  16.        {  
  17.            return AccNo;  
  18.        }  
  19.   
  20.        public string Address(string city) //Inetrface BankAddress Method  
  21.        {  
  22.            return city;  
  23.        }  
  24.    } 
In the preceding program there are two interfaces, CustomerDeteails and BankAddress, we have inherited the BankAddress interface in the CustomerDeteails interface and after implementing the CustomerDeteails interface in the class AccountInfo, we got the two methods AccountDet that are declared in the CustomerDeteails interface and Address that is declared in the BankAddress interface, so depending on the above example we can inherit two Interfaces.
 
Multiple Inheritance Using Interfaces

As we know C# does not support multiple inheritances of classes. To overcome this problem we can do multiple inheritance using interfaces, let us see how.
Example
  1. public interface IA //ineterface  1  
  2.    {  
  3.        string setName(string Name);  
  4.   
  5.   
  6.    }  
  7.    public interface IB  //Interface 2  
  8.    {  
  9.        int getAmount(int Amt);  
  10.    }  
  11.    public class ICar : IA, IB //implementation  
  12.    {  
  13.   
  14.        public int getAmount(int Amt)  
  15.        {  
  16.            return Amt;  
  17.        }  
  18.   
  19.        public string setName(string Name)  
  20.        {  
  21.            return Name;  
  22.        }  
  23.    } 
In the preceding example we have two interfaces IA and IB that both are Implemented in a class ICar by seprating commas (,). Interfaces are a very useful feature of C# , that provides the flexibility, reusability and loose coupling in many ways. Let us see some differences between classes and interfaces and abstract classes and interfaces.

Class Vs Interface

Class Interfaces
Classes have any access modifier. It cannot be allowed to be declared as private.
We can create instances of classes. Cannot create an instance of an interface.
Class members have any access modifiers. Cannot apply any access modifier to members because by default they are public.
Class methods have an implementation. Interface methods have no implementation.
A class contains any type of method. Interface has only abstract methods.
Cannot inherit multiple classes. Multiple Interfaces are inherited.

Abstract Class Vs Interface

Abstract Class Interfaces
Classes may inherit only one abstract class. Class may inherit multiple interfaces.
Abstract Class members have access modifiers. Interface members are by default public.
Abstract Class methods have implementation or no implementation. Interface methods have no implementation.
An Abstract Class contains abstract as well as non-abstract methods. Interface has only abstract methods.
When you want an implementation or no implementation of methods then use an abstract class. If you do not want any implementation of methods then use interfaces.

Summary

I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.


Similar Articles