Why Interfaces?

Posted on 2006-06-05
Last Modified: 2011-08-18

My question is simple. In what situation would I want to use an interface? I fully understand what an interface is and how they are used in code but am struggling a bit with what their use is. What task were they invented for?

Question by:pgilfeather
    LVL 9

    Accepted Solution

    Hi pgilfeather,
    Here's a recent example of my need to implement an interface (of course, this was in Java, but it's the same idea):

    I wrote a program to do steganography.  I wanted the engine to be independent of the interface, and non-interactive, so I could write both a command-line client and a GUI client and not have to change the engine code at all.  Of course, to do this, the engine has to be able to communicate with both clients.  So, I created a standard interface to be implemented by all clients to my engine.  Now the engine can accept parameters and instructions (e.g., name of input file, cipher to use), and return data (e.g., done, or error codes) without having to know whether it's the command-line client or the GUI client running, because it only uses a specific set of functions that all clients are guaranteed to implement.

    Hope that helps.


    Author Comment

    So is an interface just a way of keeping things standard ie so that it can be used between objects of different types?
    LVL 96

    Expert Comment

    by:Bob Learned
    A good use for interfaces is defining an implied "contract".  When working with controls that data-bind, such as the DataGrid, DataGridView, etc., the Datasource property can be anything that implements the IList interface.  If a class implements IList that it is assumed that the necessary functionality for working with datasources has been included.

    Realize that just because a class says that it implements all elements of the interface, doesn't mean that it has to have any code in the implemented elements.

    LVL 9

    Expert Comment

    It allows polymorphism.  One doesn't need interface inheritance to achieve polymorphism, but it sure makes it easy on unrelated classes.

    One definition of Polymorphism:

    "In object-oriented programming theory, polymorphism is the ability of objects belonging to different types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behaviour. The programmer (and the program) does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding)."

    More at


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now