What is an Interface?

Introduction

The word Interface has many meanings in the world of computer Science and its applications but it always boils down to one simple fact: It is an abstraction which helps us to understand or to reason about two different kind of systems which need to be connected somehow.

What this connection is in the end depends on the platform you use which takes advantage of the concept of an interface. But beware no interface ever describes how the operations it defines to bridge two systems are implemented.

An Interface is a black-box where you plugin some input and receives some output.  How this output is achieved  is not of concern of the user who decided to use a certain Interface.

The Contract

In the world of coding interfaces are heavily used across all kinds of different programming languages. Each representing the concept of interfaces differently.

For example Java. You have a specific construct to build so called interfaces which just tells the compiler how certain method should look like so that a normal class can then later implement these descriptions of methods; in contrast C++ has no such specific construct. You only have classes and nothing else and yet you are still able of using the concept of an interface. So we notice that interfaces are language Independent !!!

But as mentioned in the introduction you have noway of knowing HOW a certain interface achieves his task given. To put in context to Java or C++ the interfaces you define in those languages do never contain Implementation details about them.

It is the responsibility of the user of the interface to implement a given interface correctly so that two systems which rely on that interface can interact with each other.
You might ask yourself but what happens if the interface is not implemented correctly? That is very good question and the answer to that is simple:
In the best case your systems breaks down immediately.
To prevent such thing as an wrongly implemented interface, people came up with another concept on top of interfaces. Its called the contract. 

Design by Contract

A contract is nothing concrete. It is never implemented or build( except e.g. Eiffel language). In fact it is just an promise from one user (client) to an other user (supplier), only conveyed via documentation or other means how a certain functionality, “provided” by the interface, should behave if correctly implemented.
Through this promise we ensure proper behaviour between two systems.

An real world example could be the connection between a light bulb and its socket. Manufactures around the world defined a certain interface(standard) between any electrical supply to a light bulb via the socket of that bulb as well as the mount which holds a light bulb.

As long any light bulb follows the interface the manufacturer can put on top everything he wants and the bulb will work according the interface!!! The promise here would be if the connection fits a steady supply of current is guaranteed

 

One Comment

  1. Pingback: C# -> How to use IEnumerator and IEnumerable – Johannes Lüke

Leave a Reply

Your email address will not be published. Required fields are marked *