Interface
Interface in a simple term -- is a medium of communication among people. In computing, it can be a GUI interface or CLI interface to interact with the computer.
In the world of the .NET interface, it is nothing but a pure abstract class. It may contain the only declaration of:
- Methods
- Properties
- Events
- Indexers










- Implicit implementation
- Explicit implementation
Above example is of implicit implementation. Explicit implementation is used in situations where we have the same method in two interfaces and a class is implementing both interfaces. This causes ambiguity in our code. For example, in the Organism interface, we have a movement method that all organism can move and in Animal interface, we have another specialized movement method that all animals can move by running or walking. A Dog class implementing these two interfaces will not compile and gives a compile-time error that the Dog class already defines a member called ‘Movement’ with the same parameter types. Let’s have a look at the following code.
This is the Organism interface:
This is Animal interface:
This is Dog class implementing both Organism and Animal interfaces:
This is our main class Program instantiating Dog class:
If we try to run this code, it will give us the following error:
To prevent this we can implement an interface explicitly using the name of the interface in the function signatures in the derived class as follows:
One important point here is that we cannot use a public modifier with explicitly defined functions. Because they are now not directly visible to Dog class they are part of interfaces in which they are defined. And now we will call them by referencing through the appropriate interface. And does it make sense that if both methods will be public and we try to access them then which method will be invoked? And here the compiler will be confused and give us an error. So in our main method, we will call these methods as follows:
And it will produce the following output calling both movement functions without any ambiguity.
Another use of interfaces is in structures. Suppose we have a situation where we have two structures called Student and Teacher and we want to use the code defined in another structure called Person. But we cannot inherit Student structure from Person structure. Since inheritance is not supported in Structures. Here interface comes in handy. We can define Person as an interface and implement it in both Student and Teacher structures.
Use of interfaces is very simple and easy but there are some key points that we must put into consideration when using interfaces:








- It is recommended that we use the capital ‘I’ with the name of the interface.
- All the methods in an interface are public and abstract by default. We cannot use other access modifiers like private, protected, etc.
- We cannot use nested types like enumerations, structures, and classes inside the interface.
- The interface cannot inherit from structures and classes but they can implement other interfaces.
- We cannot define fields, constructors,s or destructors inside the interface.
- Interfaces cannot be instantiated but they can hold reference of all the types that implement them.
- Defining the methods in the derived types must be public. But when we explicitly implement an interface in a type then we can’t specify public access modifier in the method’s header.
- A class implementing interface can specify any method of an interface as virtual and then can override by the derived class.
- If a class or structure implements an interface then it has to implement each and every method defined in an interface. So we should keep our interface general purpose that other classes can implement.
- Also, make sure the interface should not contain too many methods because the class implementing that interface has to implement all the methods.
- Interfaces can be used to provide common functionality in the classes or structures that are not related to each other.
- Interfaces are an alternative approach to multiple inheritances in .NET.
- Interfaces are used to group objects, based on their common behaviors.
- Interfaces are used to provide polymorphic behaviors to classes because they can implement more than one interface.
Read more articles on .NET:

Sumit JoshiPosted May 11, 2016, 5:14 AM
good one
Karthikeyan KPosted Mar 22, 2016, 4:29 AM
You saved my life...Well Written
Sonu ChaudharyPosted Feb 25, 2016, 6:33 AM
good one!
Ehsan SajjadPosted Feb 18, 2016, 1:15 PM
Well Written
Sibeesh VenuPosted Feb 18, 2016, 12:10 AM
Nice Share
Gowtham KPosted Feb 17, 2016, 12:13 PM
Good One
Manoj KallaPosted Feb 17, 2016, 3:08 AM
Good one
Rajeesh MenothPosted Feb 17, 2016, 12:10 AM
Good Explanation..!! Thanks for sharing..!!
Santhakumar MunuswamyPosted Feb 16, 2016, 2:53 PM
Thanks for nice work
Humayun Kabir MamunPosted Feb 16, 2016, 5:24 AM
Nice explanation...
Asfend YarPosted Feb 16, 2016, 4:48 AM
try to work on the windows form and windows presentation foundation
Raja TPosted Feb 15, 2016, 3:01 AM
Nice Article ,Thanks for sharing
Shubham KumarPosted Feb 15, 2016, 12:46 AM
good one keep share
Sibeesh VenuPosted Feb 15, 2016, 12:35 AM
Nice Share
Debasis SahaPosted Feb 15, 2016, 12:03 AM
Nice One...
Jaipal ReddyPosted Feb 14, 2016, 11:36 PM
nice. .
Mohammed IbrahimPosted Feb 14, 2016, 11:23 PM
nice