Purpose and use of Interfaces

Sashys
Posted by in C# category on for Beginner level | Points: 250 | Views : 14872 red flag
Rating: 3.25 out of 5  
 4 vote(s)

This Article explains the Purpose and use of interfaces. We can learn when and where we need to use interfaces and how it helps us in Object oriented programming.

Introduction

Interface is basically a class which will have empty method(s), delegate(s) or event(s). The implementation of the methods is done in the class that implements the interface, that is the reason the interface's method will not have definition.


Interface will be defined using the keyword interface. Interfaces would be used as a intermediater in most of the software program.

Purpose


Interface is used for the below purposes:

  1. To maintain the Standardization or uniformity in the method definition
  2. It enables to implement Decoupling.

Standardization

It is very much possible that multiple developers write code for common operations and they will use their own naming convention for their methods. In order to maintain the uniformity in the code, we can use interfaces. The Senior developer or architect can design the interfaces with common method names, that can be used by mulitple developers. So the developers cann't use their naming conventions for the method. It brings the more readablity too.

Decoupling

Decoupling is very important in software architecture design. The system is said to be good system, when one process doesn't impact the other.So, The interface helps us to seperate one process from impacting other and the future implementation should not affect the currect process. Basically our system should be expandable with out impact.

Let us look at the first purpose : Standardaization

namespace InterfaceExample

{

    interface IDboperation 

    {

        public void insertData();

    }

    public class Dbwork

    {

        public void updatetoDb();

    }

 

    public class Dbtask

    {

        public void insertdb()

        {

 

        }

    }

}


In the Above example, there are two classes written by two different developers [Class Dbwork and Class Dbtask which as two different function. One developer named it as updatetoDb and another developer named it as insertdb. Instead, if we insist developers to implement the interface, they will use the common function called insertdata(); That way we bring uniformity and standard in our development.

Purpose of Decoupling:


namespace InterfaceExample

{

    interface ICommoanOperations

    {

        void IExportData();

    }

    public class ExporttoPdf :ICommoanOperations

    {

        public void ExportDataToPdf

        {

        //Code to Read data and export to Pdf

        }

 

    }

    public class ExporttoExcel :ICommoanOperations

    {

        public void ExportDatatoExcel

        {

         //Code to Read data and export to Excel

 

        }

    }

    public class Client

    {

        public void Export(ICommoanOperations InterfaceObj)

        {

            InterfaceObj.IExportData();

        }

 

    }

   

}

In the above example, there are two classes , one for exporting data to pdf and other for excel. Now the interface acts as a intermediater between two classes. Instead of calling the direct concrete class methods, we can call interface that calls the respective class method. This enables decoupling in our program. More over, if tomorrow , we need another fuctionality , we can attach easily.

Conclusion

It is good practice to develop code using Interfaces. And that is the reason, we implement WCF Services using Interfaces.

Page copy protected against web site content infringement by Copyscape

About the Author

Sashys
Full Name: Sasikumar Mohan
Member Level:
Member Status: Member
Member Since: 4/19/2011 1:19:47 AM
Country: India
Sasikumar .M
http://www.dotnetfunda.com
Programming is kind of Art. Let us learn it!!

Login to vote for this post.

Comments or Responses

Posted by: Akiii on: 5/1/2011 | Points: 25
@Sashys

Hi, very good article.
But i didn't understand the Decoupling thing as i am a novice one. If possible, could you please explain it in a different way....?

Thanks and Regards
Akiii
Posted by: Lokesh76 on: 5/2/2011 | Points: 25
Hi Sasikumar,
In your point number two decoupling this you made implement interface in two classes ExporttoPdf and ExporttoExcel but you did not implement your Interface method "IExportData". So you also miss the your point #1 functionality here.
Please check your code for this again...

As you are talking about secound point decoupling we can rewrite your code like this....


namespace ClassExample
{
public abstract class CommonOperations
{
public virtual void ExportData();
}

public class ExporttoPdf :CommonOperations
{
public override void ExportData()
{
//Code to Read data and export to Pdf
}

}

public class ExporttoExcel :CommonOperations
{

public override void ExportData()
{
//Code to Read data and export to Pdf
}
}

public class Client
{
public void Export(CommonOperations classObj)
{
classeObj.ExportData();
}

}

}

Posted by: Akiii on: 5/2/2011 | Points: 25
Hi lokesh......

now the code is clear......

Thanks and Regards
Akiii
Posted by: Lokesh76 on: 5/2/2011 | Points: 25
You are welcome. Please visit this link for better understanding. http://www.codeproject.com/KB/architecture/Abstract_Interface.aspx
Posted by: Akiii on: 5/2/2011 | Points: 25
excellent article lokesh...... thanks for suggesting me....

Regards
Akiii
Posted by: Akiii on: 5/2/2011 | Points: 25
HI LOKESH....

i have one doubt in the article that u provided with. I am pasting some lines...could you please explain it....?

If you want to have additional functionalist for the interface, you must implement the new ones in all of the classes that implement the changed interface. The code will not work until you make the change. But if you want to have additional functions to the Abstract class, you can have default implementation. Your code still might work without many changes.

Thanks
Akiii
Posted by: Lokesh76 on: 5/2/2011 | Points: 25
Hi Akiii,

Let me try to explain as above mention. Suppose you have a interface and implement that interface in 10 classes now you want to add one more function to interface because out of 10 classes 2 of them really required the new function functionality added by you then what will happen...
Is that possible to you to implement the new in 2 classes only ... NO it is not possible you need to implement newly added function in all the classes. So any change in Interface you need to change all classes where that particular interface implemented.

No discuss same thing with abstract class you have 10 classes in inheritance chain and you want some new functionality to add into base class or any class in Inheritance chain so I will not going to break you existing functionality at all.

Thanks
Lokesh Kumar
Posted by: Ashwin1986kumar on: 5/2/2011 | Points: 25
This is not an article... just a blog...
Posted by: Mcadeepuraj on: 5/2/2011 | Points: 25
nice artical, but what about when we add new function to the interface, for this we need to create another interface for that.
Posted by: Akiii on: 5/3/2011 | Points: 25
hi lokesh.......

thanks for the explanation......but in the article that u mentioned......its written "A non-abstract class derived from an Abstract class must include implementations for all inherited abstract methods. "

That means any member or a new member defined or declared in the abstract class have to implemented in the non-abstract class.....??

can u please clear this confusion of mine..!!

Thanks and Regards
Akiii
Posted by: Sashys on: 5/3/2011 | Points: 25
Hi Friends,

Thanks For reading the my post and comments. I didn't get a chance to go thru all your comments. Let me answer now.

Akill, You asked what is decoupling here rt - If you look at my example, Assume ExporttoPdf and ExporttoExcel are the classes builded as separate Dll. The Class Clinet can be GUI or calling program which uses that Dll.

Now, in the DLL, you have added another class called ExporttoGrid and but you call all the class methods including the new one through Interface. So , you don't have to change your client code, you can still pass the corresponding class object to interface.. This is called Decoupling as we no impact if we do any change in one functionality[dll] - We don't call the concrete class directly , we call interface from Client.

Coming to lokesh's post: Hi Lokesh, i see your code that you have replaced my interface with abstract class.. i totally agree with your code, as you told, if we happened to change the Interface, we need to confirm that all the classes implemented our interface change.
Assume another scenario, I have another class which already Inherited by ExporttoPdf and ExporttoExcel classes. Now if i use your code, ie by inheriting your abstract class CommonOperations too., will that allow? - it is becoming multiple inheritance which is not allowed in C# rt. if we want to inherit more one class we use interface rt - by keeping that scenario in the mind i used interface instead of abstract class here.. I really appriciate you interest and deciding abstrace class or interface is purly based on scenrio what we take upon.
Both of our code works - but scenrio is differnt!!
Coming to Ashwin1986kumar , He mentioned that it is not article , it is blog. ha ha. Hey ashwin, we joined here to learn technical stuff and share with our community as lokesh and akii did. we are software professionals , let us focus on technical stuff becoz, the adminstration team of this site publishes any post only after reviewing.. Let us not do their job. I still take your point positvily and try to put more stuff on my future articles.. thanks for you!!!
Posted by: Akiii on: 5/4/2011 | Points: 25
hi Sashys...

Thanks for ur response but i am little bit confused regarding decoupling...

Can you site me a different example..?

I hope i am not annoying you..!

Thanks and Regards
Akiii
Posted by: Sashys on: 5/4/2011 | Points: 25
Hi Akii, Sure, i will try some simpleExample for you.. Basically the Decoupling means, creating the application independently with more generic.. If we do any change/enhancement in one program, the client should call with out any change.. Usually , the dll or server program will have changes/enhancements frequently, but the client should be able to call the server/dll functionlity with out change.. we call it as loosly coupled. Tightly coupled means, if we change the server code, the client will also have code change.. In decoupling , we avoid it..

I will try some example for you.!!!
Posted by: S.faizaan on: 7/17/2011 | Points: 25
nice article

Login to post response

Comment using Facebook(Author doesn't get notification)