An interface acts as a contract between itself and any class or struct which implements it. It means a class that implement an interface is bound to implement all its members. Interface has only member’s declaration or signature and implicitly every member of an interface is public and abstract.
For example, the most common use of interfaces is, within SOA (Service Oriented Architecture). In SOA (WCF), a service is exposed through interfaces to different clients. Typically, an interface is exposed to a group of clients which needs to use common functionalities.
- interface IStore
- {
- void Read();
- void Write();
- }
- interface ICompress
- {
- void Compress();
- void Decompress();
- }
- public class Document : IStore, ICompress
- {
- #region IStore
- public void Read()
- {
- Console.WriteLine("Executing Document's Read Method for IStore");
- }
- public void Write()
- {
- Console.WriteLine("Executing Document's Write Method for IStore");
- }
- #endregion // IStore
- #region ICompress
- public void Compress()
- {
- Console.WriteLine("Executing Document's Compress Method for ICompress");
- }
- public void Decompress()
- {
- Console.WriteLine("Executing Document's Decompress Method for ICompress");
- }
- #endregion // ICompress
- }
Features of Interface
- An interface doesn't provide inheritance like a class or abstract class but it only declare members which an implementing class need to be implement.
- It cannot be instantiated but it can be referenced by the class object which implements it. Also, Interface reference works just like object reference and behave like as object.
- IStore IObjStore = new Document();
- ICompress IObjCompress = new Document();
- It contains only properties, indexers, methods, delegates and events signature.
- It cannot contains constants members, constructors, instance variables, destructors, static members or nested interfaces.
- Members of an interface cannot have any access modifiers even public.
- Implicitly, every member of an interface is public and abstract. Also, you are not allowed to specify the members of an interface public and abstract or virtual.
- An interface can be inherited from one or more interfaces.
- An interface can extend another interface.
- A class or struct can implements more than one interfaces.
- A class that implements an interface can mark any method of the interface as virtual and this method can be overridden by derived classes.
- Implementing multiple interfaces by a class, sometimes result in a conflict between member signatures. You can resolve such conflicts by explicitly implementing an interface member.
- interface IStore
- {
- void Read();
- void Write();
- }
- interface ICompress
- {
- void Compress();
- void Decompress();
- }
- public class Document : IStore, ICompress
- {
- #region IStore Explicit Implementation
- public void IStore.Read()
- {
- Console.WriteLine("Executing Document's Read Method for IStore");
- }
- public void IStore.Write()
- {
- Console.WriteLine("Executing Document's Write Method for IStore");
- }
- #endregion // IStore
- #region ICompress Implicit Implementation
- public void Compress()
- {
- Console.WriteLine("Executing Document's Compress Method for ICompress");
- }
- public void Decompress()
- {
- Console.WriteLine("Executing Document's Decompress Method for ICompress");
- }
- #endregion // ICompress
- }
- It is a good practice to start all interface names with a capital “I” letter.
Common design guidelines for Interface
- Keep your interfaces focused on the problem you are trying to solve and keep related tasks (methods) into a interface. Interfaces that have multiple unrelated tasks tend to be very difficult to implement in a class. Split up interfaces that contain unrelated functionality.
- Make sure your interface does not contain too many methods. Since too many methods makes implementing the interface difficult as the implementing class has to implement each and every method in the interface.
- Don't make interfaces for specific functionality. An interface should define the common functionality that can be implemented by the classes of different modules or subsystems.
When to use
- Need to provide common functionality to unrelated classes.
- Need to group objects based on common behaviors.
- Need to introduce polymorphic behavior to classes since a class can implements more than one interfaces.
- Need to provide more abstract view to a model which is unchangeable.
- Need to create loosely coupled components, easily maintainable and pluggable components (like log4net framework for logging) because implementation of an interface is separated from itself.
Disadvantage of Interface
- The main issue with an interface is that when you add a new members to its, then you must implement those members within all of the classes which implement that interface.
- Interfaces are slow as these required extra in-direction to find corresponding method in in the actual class.
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.
What do you think?
I hope, now you have got everything about Interface. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.


0 comments:
Post a Comment
Note: only a member of this blog may post a comment.