Friday, May 15, 2020

What is Interface?

What is Interface?
  1. An interface can contain signatures (declarations) of the Methods, Properties, Indexers and Events.
  2. The implementation of the methods is done in the class that implements the interface.
  3. A Delegate is a type that can't be declared in an interface. You can either use an event (if appropriate) or declare a delegate outside the interface but in the same namespace.
  4. Interfaces in C# provides a way to achieve runtime polymorphism. Using interfaces, we can invoke functions from various classes through the same Interface reference, whereas using virtual functions we can invoke functions from various classes in the same inheritance hierarchy through the same reference.
  5. An interface can inherit from one or more base interfaces.
  6. A class that implements an interface can explicitly implement members of that interface.
  7. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.
Purposes of Interfaces
  1. Create loosely coupled software.
  2. Support design by contract (an implementer must provide the entire interface).
  3. Allow for pluggable software.
  4. Allow objects to interact easily.
  5. Hide implementation details of classes from each other.
  6. Facilitate reuse of software.

No comments: