What is importance of Interface

Posted on 2006-06-04
Medium Priority
Last Modified: 2008-02-01
Hello Experts

What is importance of interface in oops. Why we use it.
What is advantages of using interface?
What is importance of interface in enterprise level application
Question by:MuhammadAdil
  • 2

Accepted Solution

PoeticAudio earned 2000 total points
ID: 16828476
Interfaces are like abstract classes, except that they don't provide ANY implementation at all, they just create a "contract" that any class that implements the interface must abide by. What the hell is the point of this?

Well, just like abstract classes (and like the example I provided in your abstract class question) it allows you to create objects dynamically and use certain functionality in these objects dynamically without knowing which object you're working with. You may wonder...

why would I use interfaces if you can do basically the same thing with abstract classes?

Well because C# doesn't support multiple inheritance (ie one class that derrives from more then 1 class simultaniously) interfaces help get around some of this. So if your object already derrives from a base class you can't have it derrive from another class simultaniously, but you can use an interface to call certain methods in these objects dynamically. Lets look at an example of this.

Lets say we have a Television object that derrives from ElectronicObject base class and a CarCigaretteLighter object that derrives from Lighter. What in the hell do these objects have in common? A TV and a CarCigaretteLighter? well, basically nothing! except... they both plug into something. A Television plugs into a wall socket and a car cigarette lighter plugs into a car cigarette lighter socket. It's not logical for these 2 objects to derrive from a single object, first because they both already derrive from other objects (ElectronicObject and Lighter) so inheritance can't be used, but we can provide an interface for both of these so we can call the PlugIn method dynamically without knowing which object we are working with. Lets check this out in code.

Pay close attention to this code and you will surely see the benefits. Interfaces used properly can create a plug-n-play type architecture which is VERY beneficial in enterprise level solutions. To be able to dynamically attach objects to eachother is a HUGE benefit which provides a loosely couple design. Very big benefits. This example doesn't get too far into all that (it's a very complex subject) but this example does show how interfaces work.

here's our interface.

interface IPlugInAble
    void PlugIn();

Okay, now lets have our Television, and CarCigaretteLighter which derrive from 2 totally different base classes. (ElectronicObject and Lighter)

class ElectronicObject
     public void TurnOn()
         System.Windows.Forms.MessageBox.Show("You have turned me on!");

class Television : ElectronicObject, IPlugInAble
    public void PlugIn()
        System.Windows.Forms.MessageBox.Show("I plugged into an electronic 3 prong socket");

Okay above we see taht Television both derrives from ElectrionicObject (so it automatically has that TurnOn method) and it implements IPlugInAble so it's forced to have the PlugIn method. Lets do our thing with CarCigaretteLighter...

class Lighter
    public void CreateFire()
        System.Windows.Forms.MessageBox.Show("You have fire, now you can light stuff");

class CarCigaretteLighter : Lighter, IPlugInAble
    public void PlugIn()
        System.Windows.Forms.MessageBox.Show("I plug into a lighter socket in your car");

Pretty similar with the CarCigaretteLighter (to keep this simple). But what we're showing here is that it derrives from a totally different class, but implements the same interface. It automatically has the CreateFire method because it derrives from Lighter which already provides the implementation.

Now lets see how we can work with this. Below we create a TV and turn it on. Then we create a CarCigaretteLighter and create some fire with it. Now since they both use IPlugInAble we know that we can plug both of them in, so lets do that as well (again, notice that the method PlugInObject doesn't care which object is being plugged in, it just knows that it can since its bound by the IPlugInAble interface. Lets look at the PlugInObject method first.

//we just pass through any object that implements IPlugInAble and this
//method knows it can call PlugIn on these objects because the objects
//that implement IPlugInAble are bound to an interface which forces them
//to have the PlugIn method.

private void PlugInObject(IPlugInAble plugInAbleObject)

Now lets provide some implementation to tie all this together (you can put this code into a button click or something)

//tv derrives from ElectronicObject, so it has a TurnOn method

ElectronicObject tv = new Television();

//carLighter derrives from Lighter so we have a CreateFire method

Lighter carLighter = new CarCigaretteLighter();

//none of the above objects have much in common, except that they plug
//into something, so they both implement IPlugInAble. We can run both
//objects through the same method and have them plug in. We perform
//a cast to do this because we create carLighter as a Lighter and tv as
//an ElectronicObject

PlugInObject(tv as IPlugInAble);
PlugInObject(carLighter as IPlugInAble);

Expert Comment

ID: 16828516
Some people that are new to these concepts question

"What is the point of an interface, it doesn't provide any implementation so I don't understand why I would ever use them, it just forces classes to have certain methods"

But as you saw above, the ability to create a contract that classes must abide by creates benefits because we can call the methods in those objects dynamically without caring which object it is. Sure this isn't a huge code saver (well it is, actually) but it doesn't seem so, really. Abstract classes help cut down code and logically group objects, but interfaces are more for designing flexible architectures, not so much for really saving code, it's more of a design paradigm rather then a "cut as much code as possible" paradigm. With the proper use of interfaces you can create a VERY flexible architecture. Just remember that interfaces are more for providing immensly flexible architectures and abstract classes are more for stripping out common functionality and throwing it into 1 base class so you can cut code down and make it much easier to build other objects that derrive from that base class (and building more logical designs). These are just a few benefits.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Integration Management Part 2
Loops Section Overview
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question