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.
- interface MyInterface
- {
- void DefineYourMethodsDefination();
- }
- Let’ s see the details example of Interface with complete implementation:
- Here is my Interface
- interface MyInterface
- {
- /// <summary>
- /// Property in Interface
- /// </summary>
- public int MyProperty
- {
- get;
- set;
- }
- /// <summary>
- /// Methods in Interface
- /// </summary>
- void DefineYourMethodsDefination();
- /// <summary>
- /// Event in Interface
- /// </summary>
- event EventHandler DoSomething;
- }

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

So that means we can’t create an object of an Interface. - No need to provide access modifiers,

In case you will try to do so, you will get the following error,
That means by default everything inside Interface is Public. - I have already explained how we can define Methods, Properties, and Events in the Interface.

- 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,
- public class Implementinterface: MyInterface, MyInheritInterface
- {
- public int MyProperty
- {
- get
- {
- return MyProperty;
- }
- set
- {
- MyProperty = value;
- }
- }
- public void DefineYourMethodsDefination()
- {
- Console.WriteLine("Hi Nishant");
- Console.ReadLine();
- }
- public event EventHandler DoSomething;
- public void ActiveDoSomething()
- {
- //DoSomething += Implementinterface_DoSomething;
- if (DoSomething != null)
- {
- DoSomething("Nishant", null);
- }
- }
- /// <summary>
- /// ToDo : Implement the same
- /// </summary>
- public void ImplementInheritance()
- {
- throw new NotImplementedException();
- }

Nishant MittalPosted Jul 6, 2016, 11:18 PM
Thanks All....
Suman SauravPosted Jul 6, 2016, 2:26 AM
Great Article!!
SubashPosted Jul 4, 2016, 8:45 AM
Nice
Santhakumar MunuswamyPosted Jun 25, 2016, 2:21 AM
Thank you for nice article
RajaPosted Jun 20, 2016, 4:35 AM
Good One...
Vignesh ManiPosted Jun 18, 2016, 3:17 PM
Nice
Humayun Kabir MamunPosted Jun 18, 2016, 11:46 AM
Nice...