Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

What is the real use of interfaces in Java? What is meant by programming to interfaces?

I heard these things several times but I don't know what it is and why it is used.

share|improve this question

marked as duplicate by gnat, BЈовић, m3th0dman, Jim G., maple_shaft Aug 5 '13 at 10:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Not that programming to an interface does not obligatory mean an actual java interface, it may also be a class. –  Kemoda Aug 5 '13 at 6:10
    
I didn't get you - Kemoda –  Anoopss Golden Aug 5 '13 at 6:13
    
@AnoopssGolden see interface as some sort of contract, a class can also provide this sort of contract. For instance there is no point in defining a "Java interface" for certain types of model objects (because, frankly, it is an overkill). But still they provide an interface –  Kemoda Aug 5 '13 at 6:24
add comment

3 Answers 3

One day, a junior programmer was instructed by his boss to write an application to analyze business data and condense it all in pretty reports with metrics, graphs and all that stuff. The boss gave him an XML file with the remark "here's some example business data".

The programmer started coding. A few weeks later he felt that the metrics and graphs and stuff were pretty enough to satisfy the boss, and he presented his work. "That's great" said the boss, "but can it also show business data from this SQL database we have?".

The programmer went back to coding. There was code for reading business data from XML sprinkled throughout his application. He rewrote all those snippets, wrapping them with an "if" condition:

if (dataType == "XML")
{
   ... read a piece of XML data ...
}
else
{
   .. query something from the SQL database ...
}

When presented with the new iteration of the software, the boss replied: "That's great, but can it also report on business data from this web service?" Remembering all those tedious if statements he would have to rewrite AGAIN, the programmer became enraged. "First xml, then SQL, now web services! What is the REAL source of business data?"

The boss replied: "Anything that can provide it"

share|improve this answer
    
very good! Of course inevitably it leads to "the application must be able to convert anything to anything else without knowing at conversion time what it actually is converting and how to map it" (which I've actually seen). –  jwenting Aug 5 '13 at 6:13
    
And the moral of the story is: If the dev had originally defined an IDataSource interface which XmlDataSource implemented, adding support for SqlDataSource and WebServiceDataSource wouldn't have required if-statements. –  MetaFight Aug 5 '13 at 10:42
add comment

You program to an interface because it's easier to handle. The interface encapsulates the behavior of the underlying class. This way, the class is a blackbox. Your whole real life is programming to an interface. When you use a tv, a car, a stereo, you are acting on its interface, not on its implementation details, and you assume that if implementation changes (e.g. diesel engine or gas) the interface remains the same. Programming to an interface allows you to preserve your behavior when non-disruptive details are changed, optimized, or fixed. This simplifies also the task of documenting, learning, and using.

Also, programming to an interface allows you to delineate what is the behavior of your code before even writing it. You expect a class to do something. You can test this something even before you write the actual code that does it. When your interface is clean and done, and you like interacting with it, you can write the actual code that does things.

share|improve this answer
add comment

An interface is a simple contract.

The interface defines methods and you can call methods on any object that implements this interface. It does not matter which object this is.

Also an object can implement multiple interfaces and therefore you can see this as implementing different contracts.

By programming against the interfaces you do not depend on a certain class and can replace the implementation without changing the rest of you code.

As example:

If you use List in your application you can switch between LinkedList and ArrayList without changing your code.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.