Interface is the term which is most widely used in my previous tutorial and many of newbies do not understand that what the interface is. So for them I thought to explain the interface so that all of you could have good basic concepts of object oriented programming.

 Interface

Interface is the publicly exposed behavior using which outer world or other objects interact with the object and can access their private information. As we know information hiding does not allow us to access the private data directly so question is then how to access this private data. So to access this hidden data every
Object has exposed its behavior to the outside world. So using this behavior we can access the private or hidden data.

We can make the behavior exposed by using public keyword in the class. we will study about access
specifier in our next tutorials.

Now let's take some examples from the real life to explain the concept of interface. Consider your mobile phone as an object now you need to dial a phone most probably you will open the dial box and will start the dialing the number. Now this dial box is the example of interface which mobile phone has provided you so you can access the private data. Mobile phone internally retrieves the data to do so but in order to avoid you from all this complexity object has provided you with an interface.

Let's take another example of interface. Consider Vehicle as a class and its object car. Now car object has many attribute like speed. To increase or decrease its speed car object has provided you an interface accelerator using which you can accelerate your car.

Implementation


In object oriented programming implementation describes the functionality of the interface or behavior. Interface is the only describe as service offered but how this service works or functions? It is depend upon the implementation details. Implementation details tell us that in that way a service is being implemented.

Consider an example, you have  a car which has interface accelerator to accelerate car but how car will accelerate using interface accelerator. What will be the internal mechanism of its functionality .This all will be implementation details which all are hidden from you. Now consider car provided you with another interface called brakes you can use brakes to access the functions of brakes which is to stop the car. Now how it will apply brakes what will be the complex mechanism? This is all implementation.

   Separation Of Implementation And Interface

Interface and implementation are separated from each other in  object oriented programming. The separations of implementation mean that with changing implementation interface will not be affected. This is achieved with the help of data hiding and encapsulation.

Let's take some examples to elaborate this point.
     
You have a car which has provided you with the interface brake now how the brakes are applied and what is the force of résistance which stop the car from moving and all its implementation details are hidden from you. You are only concerned with the interface while implementation are made hidden from you this thing avoids complexity. You do not need to know these extra details so object provided you only an interface to use these implementations.

Consider another example of mobile phone, mobile phone has provided you with an interface of dial box to enter the number you want to connect. But how internally the number is dialed and how you’re mobile is fetching the characters from your keyboard and then displaying them on the screen. This all complex phenomena are hidden from you. You are not concerned with the complex implementation detail.

Infect after reading these examples you will understand that how interface is separated from the implementation details.


        Advantage of Separating Interface And Implementation Details



(1) Separating Interface from implementation will avoid the complexity of implementation  details.
     
  (2) A change in implementation detail will not affect the interface.

   
Let me explain my second point. Suppose you have two cars one run on the diesel and other run on CNG. You may have notice both cars stop with the brakes and both car accelerate with accelerator although their implementation details are different. Car which is running with petrol have different internal mechanism and car with CNG has different mechanism but their interface is same. So implementation is different but interface is same. Moreover this is because it will not be desirable to change the interface also because it will cause the confusion among the user. Consider how confuse you will be when your car running on CNG is stopped by brakes and your car running from petrol is stopped by using some other interface.