Interface In C#

Key Points:

  1. An interface contains only the signatures of methods, properties, events or indexers.

  2. Interfaces cannot contain constructors.

  3. Interfaces cannot contain fields.

  4. By default Interfaces are internal we can make it public ,but not private, protected, or protected internal.

  5. The modifier are not valid for this items inside the Interface ,by default all items have "public" as modifier.

  6. Interface can be implement by other Interface but not by Other class, Ex:interface IDoctor: ITeach.

  7. The class which implements interface, should implement all members of the Interface, else it results in compilation error.

  8. Interfaces cannot be initialized directly, since they do not have a definition for members, they should be implemented by either a class or structure.

Why Interfaces

The OOPS is gifted with a concept called Inheritance, but Inheritance has some limitations, like in multiple inheritance we are't able to create a derived class from two base classes, since it confuses the compiler.

We will discuss it briefly with following example:

  1. class IndianFather  
  2. {  
  3.     void setColour()  
  4.     {  
  5.         Console.WriteLine("Setting the Indian Colour");  
  6.     }  
  7. }  
  8. class ForeignFather  
  9. {  
  10.     void setColour()  
  11.     {  
  12.         Console.WriteLine("Setting the Foreign Colour");  
  13.     }  
  14. }  
  15. class Child: IndianFather, ForeignFather  
  16. {}  
From above example we can clearly know that a derived class cannot have multiple base classes, so we can put the method setColour() into a interface as follows:
  1. namespace InterfaceDemo  
  2. {  
  3.     interface IColour  
  4.     {  
  5.         void setColour();  
  6.     }  
  7. }  
Now we can implement IColour interface in two different classes IndianChild and ForeignChild.
  1. class IndianChild: IndianFather, IColour  
  2. {  
  3.     public void setColour()  
  4.     {  
  5.         Console.WriteLine("Setting the Indian Colour");  
  6.     }  
  7. }  
  8. class ForeignChild: ForeignFather, IColour  
  9. {  
  10.     public void setColour()  
  11.     {  
  12.         Console.WriteLine("Setting the Foreign Colour");  
  13.     }  
  14. }  
Suppose we have same method names in two different Interfaces, then we implement such methods with Interface name as prefix like:

void IColour.setColour(), this kind of implementation is called as Explicit Implementation.

We are not provided with option like Explicit Inheritance, so interfaces comes into the picture.I n C# multiple inheritance is not allowed, Conversely it is achieved by interfaces like, a class can implement more than one interface.

I am here explaining the real time example of Interfaces. Consider a hospital, which consists of various departments like Reception, Out Patient Department (OPD).

In reception department a guy who collects information from patients get admission of that patient, then he allocates the specific department to the patients viz OPD,Cardiology.

Now we create our first interface for the "Reception Department".
  1. namespace InterfaceDemo  
  2. {  
  3.     interface IReception  
  4.     {  
  5.         void getAddmission();  
  6.         void allocateDepartment();  
  7.     }  
  8. }  
Now we create our second interface for the "Treat Department".
  1. namespace InterfaceDemo  
  2. {  
  3.     interface ITreat  
  4.     {  
  5.         void doOperation();  
  6.         void suggestPrecription();  
  7.     }  
  8. }  
We will create a class called Hospital, which implements our Interfaces IReception and ITreat.
  1. namespace InterfaceDemo  
  2. {  
  3.     class Hospital: IReception, ITreat  
  4.     {  
  5.         public void getAddmission()  
  6.         {  
  7.             Console.WriteLine("Getting addmission of patients");  
  8.         }  
  9.         public void allocateDepartment()  
  10.         {  
  11.             Console.WriteLine("Allocation department to patients");  
  12.         }  
  13.         public void doOperation()  
  14.         {  
  15.             Console.WriteLine("Doing operation to patients");  
  16.         }  
  17.         public void suggestPrecription()  
  18.         {  
  19.             Console.WriteLine("Suggesting prescription to patients");  
  20.         }  
  21.     }  
  22. }  
If we instantiate our Hospital class then, we are done with interfaces:
  1. namespace InterfaceDemo  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Hospital objHospital = new Hospital();  
  8.             objHospital.getAddmission();  
  9.             objHospital.allocateDepartment();  
  10.             objHospital.doOperation();  
  11.             objHospital.suggestPrecription();  
  12.         }  
  13.     }  
  14. }  
Now this is the simple example of interfaces, but just imagine a real world scenario, where we want to instantiate a object for Reception Department, then if we follow the above steps, like:
  1. namespace InterfaceDemo  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Hospital objReceptionist = new Hospital();  
  8.             objReceptionist.getAddmission();  
  9.             objReceptionist.allocateDepartment();  
  10.             objReceptionist.doOperation();  
  11.             objReceptionist.suggestPrecription();  
  12.         }  
  13.     }  
  14. }  
The above code will provide all permissions for the receptionist viz getAddmission, allocateDepartment, doOperation, suggestPrecription. this will create a great disaster, since here receptionist is able to make operations as well as suggesting prescriptions. So we have to pull back receptionist to do only his work; the below code will do this work.
  1. namespace InterfaceDemo  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             IReception objReceptionist = new Hospital();  
  8.             objReceptionist.getAddmission();  
  9.             objReceptionist.allocateDepartment();  
  10.         }  
  11.     }  
  12. }  
Now on wards the Receptionist will do only his work. Great,  we successfully pulled him back, now we create a doctor to treat patients.
  1. namespace InterfaceDemo  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ITreat objDoctor = new Hospital();  
  8.             objDoctor.doOperation();  
  9.             objDoctor.suggestPrecription();  
  10.         }  
  11.     }  
  12. }  
Now we come across a use of Interfaces, now we look at one more vital essence of Interfaces.

As you all know about our first Interface IReception, it includes two implementationless methods like getAddmission(), allocateDepartment(), now we use same Interface IReception in our new class called College.

The college class implements one more Interface called ITeach, its declarations is as follows:
  1. namespace InterfaceDemo  
  2. {  
  3.     interface ITeach  
  4.     {  
  5.         void teachStudents();  
  6.         void conductExams();  
  7.     }  
  8. }  
The class college implements IReception and ITeach as below
  1. class College: IReception, ITeach  
  2. {  
  3.     public void getAddmission()  
  4.     {  
  5.         Console.WriteLine("Get Addmission in to College");  
  6.     }  
  7.     public void allocateDepartment()  
  8.     {  
  9.         Console.WriteLine("Allocation department for students");  
  10.     }  
  11.     public void teachStudents()  
  12.     {  
  13.         Console.WriteLine("Teach to students");  
  14.     }  
  15.     public void conductExams()  
  16.     {  
  17.         Console.WriteLine("Conducting Exams to students");  
  18.     }  
  19. }  
In class College we implement the same methods of IReception with different implementation, depending on the context, since Interfaces keep themselves open for a different kind of implementation on different contexts.

Class College instantiates in the same manner as Class Hospital.
  1. namespace InterfaceDemo  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             College objCollege = new College();  
  8.             objCollege.getAddmission();  
  9.             objCollege.allocateDepartment();  
  10.             objCollege.teachStudents();  
  11.             objCollege.conductExams();  
  12.         }  
  13.     }  
  14. }  
Cool we are done with this. Thank you.

Guys please put comments, it will help me to improve.

 


Similar Articles