Interface in C#

An Interface is a collection of data members and member functions but it does not implement them. The methods defined in an Interface do not have implementation, they only specify the parameters that they will take and the types of values they will return. An Interface is always implemented in a Class.

An Interface can have same access modifier as a class, such as Public and Private.
Interfaces are introduced to provide the feature of multiple inheritance to classes. Multiple Inheritance is the feature of object oriented programming which allows a class to inherit from multiple classes.
Points to Remember :-

  • An Interface is declared using the interface Keyword Interfaces behave as templates that have method declarations in them .
  • A Class that implements an interface must implement all of its methods , otherwise an error occurs.
  • To implement multiple inheritance in a program you need to use interfaces. Implementing an interface is similar to inheriting a class.
  • When an interface is implemented by a Base Class , then the derived class of the base class automatically inherits methods of the interface.
  • You can initialize an object of the interface by type casting the object of the derived class with the interface itself.
  • An interface contains only the signature of the methods and not the body of the methods. The methods are implemented in the class that inherits the interface.
  • An interface can be inherited by another interface.

Below we have created a Interface and named it as IMyinterfaceName and inside the Interface we define a method named as myfirstMethod.

                
 Interface IMyinterfaceName
 {
    Void myfirstMethod();
 }                 
                

Difference between Interface and Abstract Classes in C# ? Describe it in details.

There are few difference between Interface and Abstract Classes , they are listed below.

  • Abstract Classes can have implementation for some of its members but the Interface can not have implementation for any of its members.
  • Interface can not have fields whereas an Abstract Class have fields.
  • An Interface can inherit another interface only , and can not inherit from an abstract class. whereas an abstract class can inherit from another abstract class or another interface .
  • An interface can inherit from multiple interfaces at the same time, whereas a class can not inherit from multiple classes at same time.
  • Abstract class members can have access modifiers whereas interface members can not have access modifiers.

When do you choose Interface over an Abstract Class or vice versa ?.

If you have implementation that will be the same for all the derived classes , then it is better to go for an abstract Class instead of an Interface. so you can share implementation for all derived classes in one central place, and avoid code duplication in the derived classes. rather than you have an interface you can move your implementation to any class that implements the interface.

Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.