Interfaces in C#

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 .
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.
Interfaces are declared using the interface keyword as in the following:
interface [Interface Name]
public interface IA //ineterface
    string GetName(string a);  // Interfaces cannot contain body of its method
 public class ICar:IA //implementatin --interface implemented by using class ICar
     public string GetName(string a)  
         return a;  
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.
public interface BankAddress //Interface 1  
       string Address(string city);     
   public interface CustomerDeteails : BankAddress //Interface BankAddress inherited  
       int AccountDet(int AccNo);  
   public class AccountInfo : CustomerDeteails ////Interface CustomerDeteails Implemented  
       public int AccountDet(int AccNo)  //Inetrface CustomerDeteails Method  
           return AccNo;  
       public string Address(string city) //Inetrface BankAddress Method  
           return city;  
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.
public interface IA //ineterface  1  
       string setName(string Name);  
   public interface IB  //Interface 2  
       int getAmount(int Amt);  
   public class ICar : IA, IB //implementation  
       public int getAmount(int Amt)  
           return Amt;  
       public string setName(string Name)  
           return Name;  
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.
I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.

Post a Comment

Protected by Copyscape Online Copyright Protection
Copyright © Compile Mode