When to Use Interface And When To Use Abstract Class - Part Two

Please go through my previous article for better understanding on Abstract Class:

I got a lot of feedback on my previous article and some users are desperately waiting for this one. I will try to incorporate all the requirements and especially when and where to use Abstract class and Interface.

Let us start with Interface now.

Interface

Readers might have heard about runtime polymorphism. Interface is the perfect example to achieve runtime polymorphism. Interface behaves like a class but with no implementation of its methods. Interfaces defines properties, methods, delegates, and events which are the members of the interface. Interfaces contain only the declaration of the members. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.

  1. interface MyInterface  
  2. {  
  3.     void DefineYourMethodsDefination();  
  4. }  
  5. Let’ s see the details example of Interface with complete implementation:  
  6.     Here is my Interface  
  7. interface MyInterface  
  8. {  
  9.     /// <summary>  
  10.     /// Property in Interface  
  11.     /// </summary>  
  12.     public int MyProperty   
  13.     {  
  14.         get;  
  15.         set;  
  16.     }  
  17.   
  18.     /// <summary>  
  19.     /// Methods in Interface  
  20.     /// </summary>  
  21.     void DefineYourMethodsDefination();  
  22.   
  23.     /// <summary>  
  24.     /// Event in Interface  
  25.     /// </summary>  
  26.     event EventHandler DoSomething;  
  27. }  
Let us see the implementation in the class as well below,

class

Now we move on and see a few important things about “interface,” let’s take it one by one. 
  1. Try creating instance of Interface,

    Interface

    So that means we can’t create an object of an Interface.

  2. No need to provide access modifiers,

    modifiers

    In case you will try to do so, you will get the following error,

    error

    That means by default everything inside Interface is Public.

  3. I have already explained how we can define Methods, Properties, and Events in the Interface.

    Interface

  4. Now when it comes to Inheritance, there is no other way to implement inheritance except Interface in C#.

I have two interfaces and I am implementing both below,

  1. public class Implementinterface: MyInterface, MyInheritInterface  
  2. {  
  3.         public int MyProperty  
  4.         {  
  5.             get  
  6.             {  
  7.                 return MyProperty;  
  8.             }  
  9.             set  
  10.             {  
  11.                 MyProperty = value;  
  12.             }  
  13.         }  
  14.   
  15.         public void DefineYourMethodsDefination()  
  16.         {  
  17.             Console.WriteLine("Hi Nishant");  
  18.             Console.ReadLine();  
  19.         }  
  20.   
  21.         public event EventHandler DoSomething;  
  22.   
  23.         public void ActiveDoSomething()  
  24.         {  
  25.                 //DoSomething += Implementinterface_DoSomething;  
  26.                 if (DoSomething != null)  
  27.                 {  
  28.                     DoSomething("Nishant"null);  
  29.                 }  
  30.             }  
  31.             /// <summary>  
  32.             /// ToDo : Implement the same  
  33.             /// </summary>  
  34.         public void ImplementInheritance()  
  35.         {  
  36.   
  37.             throw new NotImplementedException();  
  38.         }  
This is all about interface. In the next article will explain the scenarios when we have to go for Interface and when we have to go for Abstract Class.


Similar Articles