Social Icons

Thursday, 11 July 2013

Interfaces in c# why we use interfaces and how interfaces are different from abstract classes

Interfaces in C# and when to use Interfaces

Introduction:

In my previous articles i explained various Object Oriented Programming concepts like Static Classes in c#,Abstract Classes in c#,Sealed Classes in c# and Usage, Partial classes in c# and Usage and What are the access specifiers in C# and Various articles on WPF and ASP.NET.

           Now we will see the common interview question that could arise for all people not only freshers or experienced candidates is what is an interface,when to use interface or why we use interface and why can't we use an Abstract Class.

Description:

What is an Interface:

     It is also a type like class but it only contains Abstract members. And these Abstract members must be implemented in child classes.Interface will contain function prototypes and a class that inherits this interface needs to define these functions and then consume them.
           
             Interfaces can not contains fields i.e variables.Interfaces can contain events,indexers,methods and properties.Interface methods can not have body i.e members can not have definition.An interface can't be instantiated directly. Its members are implemented by any class or struct that implements the interface.

            Interface is a contract between itself and any class that implements it.This contracts states that the class or struct which implements it must implement it's members i.e interface's methods, properties and events.
we can declare an interface with Interface keyword syntax:

Important things about interfaces:

  • According to interface definition it looks like Abstract Class but unlike Class or Abstract Class it can not do any thing, it defines what a class that implement it will do.
  • The class which implements it needs to implement interface members with public keyword.
  • Interface members can not accept public modifier.
  • A class or struct can implement multiple interfaces. A class can inherit a base class and also
    implement one or more interfaces.
  • Interfaces provide a way to achieve runtime polymorphism.

Why we need to use Interfaces:

     One benefit of using Interfaces is it provides multiple inheritance. All classes in c# exactly have one base class only but a class can inherit multiple interfaces.

   According to wikipedia there another two uses of interfaces.

1. Interfaces are used to encode similarities which the classes of various types share, but it do not require relationship with class.
   For example a human and a parrot can Whistle, however it is not make sense to represent Humans and Parrots as sub classes of whistler Class. Rather Parrot is sub classes of Animal class, so here Whistler is an interface where Human and Parrot classes will implement. It is the perfect example we can say in interviews if they ask us why we use interfaces why can't we use class or abstract class i thought it is in sense of declaring.

 2. Another use of interface is being able to use object without knowing it's type of class, but rather only that implements a certain interface. For example if we are annoyed by whistling noise, we may not know whether it is Human or Parrot, we can think that a whistler is whistling.

Example for using Interfaces:

In market different types of cars available where all cars have the same type of prototypes i.e parts. According to Interface definition it contains Function Prototypes so we can define a Interface called Car so that all Brands of classes can implement that.

Conclusion:

 Interface based programming provides loose coupling, component based programming, easily maintainability and Extensibility. Don't forget to comment and share for your friends if you like this post.


Related Articles:

4 comments:

  1. nice article
    simple and helpful example!!
    u may also read this:
    http://www.mindstick.com/Articles/340a0bac-c51c-4d44-bc64-25d2d51a71dd/Interfaces

    ReplyDelete
  2. Replies
    1. Very interesting & good Example why we use Interface

      Delete

Please Give your Valuable Suggestions,Questions and Comments to improve my blog.