October 27, 2013

Interface and Implementation in OOP

Interface is a set of functions of an object that he wants to expose to other objects. Interface of an object provides us the list of available functions.
As we discussed previously that data and behavior of each object is hidden in that object itself so we have to use the concept of interface of the object to expose its behavior to outer word objects.
· Different objects may need different functions of an object so interface of an object may be different for different objects.
· Interfaces are necessary for object communication. Each object provides interface/s (operations) to other objects through these interfaces other objects communicate with this object.
Example – Interface of a Car
· Steer Wheels
· Accelerate
· Change Gear
· Apply Brakes
· Turn Lights On/Off
Example – Interface of a Phone
· Input Number
· Place Call
· Disconnect Call
· Add number to address book
· Remove number
· Update number

It is actual implementation of the behavior of the object in any Object Oriented language.
It has two parts,
· Internal data structures to hold an object state that will be hidden from us it will store values for an object data members.
· Functionality in the form of member functions to provide required behavior.
Examples of Implementation
  1. Gear Box in car system
Consider object Gear Box in car system it has a certain structure and functionality. When this object will be implemented it will have two things,
· Physical structure of the gear box
· Functionality implemented in this structure to change gear.
Both these things are part of implementation.
So it has,
· Data Structure in the form of Mechanical structure of gear box
· Functionality mechanism to change gear

  1. Address Book in a Phone
Similarly take the example of contact details saved in the SIM of a phone, In that case we can say physical structure of SIM card as Data Structure
And Read/write operations provided by the phone as Functionality.
 
As discussed earlier we only show interface of an object to outside world and hide actual implementation from outside world. The benefit of using this approach is that our object interface to outside word becomes independent from inside implementation of that interface. An object may have more than one interface.
This is achieved through the concepts of encapsulation and information hiding. Interface and implementation are separated from each other to achieve Information Hiding.
 
Real Life example of separation of interface and implementations
Driver has a standard interface to drive a car and using that interface he drive can drive any car regardless of its model or type whatever engine type it has or whatever type of fuel it is using.
There is another important phenomena in OOP , i.e. ‘messages’.  
Objects communicate through messages they send messages (stimuli) by invoking appropriate operations on the target object. The number and kind of messages that can be sent to an object depends upon its interface. Objects communicate with each other using messages.
Examples – Messages
A Person sends message (stimulus) “stop” to a Car by applying brakes
A Person sends message “place call” to a Phone by pressing appropriate button



1 comment:

C program to Read From a File

#include <stdio.h> #include <stdlib.h> void main() {     FILE *fptr;     char filename[15];     char ch;   ...