Wednesday, July 30, 2014

Why to use an Interface?

While doing object oriented programming we generally make the classes and provide the functionality in it to give to the outside world.

That class may contain some private members and methods and some public members, properties and methods to use the functionality. While accessing the class members and methods we create object of the class and initialize it to call the methods.

This is the very basic development approach. We generally tend to ignore the usage and importance of Interface. An interface can provide us the flexibility of better utilization of memory and structure declaration for the members of the class. Also at the same time we can better use the concept of object oriented programming.

By use of an interface to implement over the class we can achieve the followings
è Encapsulation
è Polymorphism
è Better usage of memory
Here is the list of objects which are available to outside world by using of an interface
è Public properties
è Public methods
è Events
è Indexers
For example :

Public interface IMyFunctions
{
public string inputValue{get;set;}
     Void DoSomething();
}

Class MyFunctions : IMyFunctions
{
public string inputValue{get;set;}
     public Void DoSomething()
{ // provide your logic here }

}

Class OutputClass
{
IMyFunctions myFunctions = new MyFunctions();
myFunctions.inputValue = “TestData”;
muFunctions.DoSomething();
}


Since C#.Net does not support multiple inheritances of the classes, so somehow by using implementing multiple interfaces on a single class we can achieve the same.

No comments:

Post a Comment

Put your comments here

Motivational qoutes

पूरे विश्वास के साथ अपने सपनों की तरफ बढ़ें। वही ज़िन्दगी जियें जिसकी कल्पना आपने की है।