What is an interface?

Why coding to an interface matters, but wait what is an interface actually? In simple terms please.


Right context setting is very important to understand this concept. Think in terms of what our end goal is, rather than specific language oriented solution.

Most often while reading design patterns, we come across the guidance, that says program to an interface not to an implementation.

At this time many of us may think that the language that I code in doesn't support interface, hence it's not for me.

Now this the worst part and the thing, that most often the JAVA context is set even before we talk about design patterns or coding principles. The reality is that, this guidance has nothing to do with the JAVA interface construct.

Interface is like a physical ticket counter. When you know that you have to buy a ticket, you know that you have to go to the counter and tell what you need, you also have certain expectation of what you would get in response, like a printed ticket or a band etc.

For you as an end user does it doesn not matter who sits behind the counter? (if it does you have certain bigger issues to worry)

As long as you get your job done it should not matter for you, who is behind the counter and in that way a male can be replaced by female or transgender or even a robot whichever business decides is good for the business, customer experience and for overall evolution.

Here you are interacting with a counter interface to perform certain actions and you also have certain response expectations, you have to mimic the same thing in your code that's all.

Who cares if your language supports and ugly confusing abstract class or an interface. Though there are some technical advantages associated with these language constructs while trying to implement this pattern.