Sunday, 20 September 2015

What is Interface and Implementation And what is the separation of implementation and interface

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.


What is Encapsulation In C++ programming

We have studied about basic design principles of object oriented programming. After explaining, Information hiding now we will move on to another design principle called Encapsulation.

Encapsulation is defined in two ways

Encapsulation


(1) The tight coupling of attributes and behavior with in an object is called encapsulation

(2) Separation of information structure and implementation details is also called     encapsulation.

Now we will first explain the First definition:

According to the first definition the coupling of data members and members function in one place is called encapsulation. An object has attributes and behavior encapsulate within it.

In procedural C++ programming data and functions are separated which cause a lots of problem in debugging and code management .Object oriented programming has provided the concept of encapsulation to use the data and functions, which operate on that data, effectively. Encapsulation thus avoids the complexity and help in code management.

Now let's take an example from real life about encapsulation, Recall the previous examples which I have given you about Information hiding. In information hiding i have told you that information is hidden from the outside world and to access them you need a member function. Now this is a classic example of encapsulation, you can see that to access the data you need a function, both data and function work together.

Let me give you another example to explain my point further. Consider yourself as an object you have all necessary gradient to be an object. You have many attributes but for sake of simplicity we only consider your one attribute which is position. You remain in certain position during your work or during your rest. Consider you have a behavior move now in order to manipulate the position you will use your function move. Behavior Move and attribute position both things are tightly coupled with in you(consider yourself an object).You  may not know that how you change position what is the force which drive you to move. This is because implementation detail about your behavior move is hidden. We will further study in detail about implementation and interface in our next lecture.


Now let's describe the encapsulation in term of second definition. According to this definition which some C++ book used, Encapsulation is the separation of information structure and implementation details.

Let’s take some example from the real world to elaborate this concept further. A Phone stores phone numbers in digital format and knows how to convert it into human-readable characters. Consider phone as a class and consider and suppose iphone is an object of class phone. Iphone object has behavior convert. We the outside world does not know at all how the mobile phone objects convert the machine code into human readable format.

This is the separation of information structure and implementation detail. We do not know that how mobile phone object implement its behavior convert.

These both definitions are used but I will refer you to use first one because it is more comprehensive and also most used. There are some points where programmer and researcher do not agree  and this is the example of it. So you do not need to worry about that.


Advantages of Encapsulation in C++


  (1) Encapsulation will bring simplicity to your program .You will not need to write separate data for every function.

(2)Encapsulation will avoid complexity as your program will be easier to understand and debug.

Note::

     If you have further queries about “concept of Encapsulation” you can email me @jawad1647@gmail.com, I will try to solve all your queries and also please don’t hesitate to report mistakes as you know "to err is a human". You can report the errors by sending me a mail at the email address given above.




Saturday, 19 September 2015

What is Information hiding in object oriented programming?

After discussing the c++ class and objects in detail now we will move toward the design principles of object oriented programming.

Design Principles of Object Oriented Programming:

  
There are 5 design principle of object oriented programming as

   (1) Information hiding
   (2) Encapsulation
   (3) Abstraction
   (4) Inheritance
   (5) Polymorphism

  These 5 concepts are the back bone of object oriented programming and we can say its foundation is based on these principles.

I know it is very difficult for the newbies to grasp these concepts so I will try to explain these principles in details and by giving relevant examples from the real life.

Let’s first explain the information hiding.

Information Hiding

Information hiding is the technique in which we try to hide the attributes of the object from outside world.
According to this technique information in whatever form is stored inside the object and it is hidden from the outside world and only object itself manipulate its state or data and we cannot directly access them. We need an interface to access them.

Information hiding is achieved by using access specifies a topic we will study in details in upcoming tutorials.

    Examples of Information Hiding


 Information Hiding has plenty of examples related to the real life.

  Suppose myself jawad tariq is an object. Human is a very complicated object because it has every requirement  which is needed for an object.  Now I use information hiding technique as you the outer world do not know about my personal life. You do not know from where i belong, you do not know my marital status and you do not know either I have kids or not. This is because I have made all these information hidden. This information is saved inside my brain at some part of it and you the outer world have no direct access to it. I have not put a label on my forehead which tell you what is my age or what is marital status etc. You can only access to my private data using the interface I have provided you .Consider that I  have provided you an interface to find my age "What is your age" you  can access to my private data indirectly by using that interface. So object has made all its information hidden so other objects cannot directly manipulate their states.


Information Hiding Advantages


    (1) Information hiding detail simplifies the modal by hiding complex details and implementation               from you.

    (2) Information hiding technique will not allow the user to manipulate states of object directly hence this technique avoids the object to go into the invalid state.

Let me elaborate my second point with some examples.  Consider a class student for sake of simplicity considers it has only one attribute age. Now what will happen if user enters   a negative value for age?  User may know that age is always in positive integer but he may accidently or unintentionally press negative key. So this will leads the object in to invalid state. Here information hiding is useful as it do not give direct access to the user to manipulate the data. User can only manipulate the data using an interface or a member function. Now you may have a question that how member function will act to reduce chances of object to go into invalid state. I will explain this when we will go further into object oriented programming. At that moment we are only discussing the concepts of object oriented programming which I think are very necessary for you to feel comfortable with object oriented programming in c++.


Note:      

          If you have further queries about “concept of objects” you can email me at jawad1647@gmail.com, I will try to solve all your queries and also please don’t hesitate to report mistakes as you know "to err is a human". You can report the errors by sending me a mail at the email address given above.

what is object?(examples from the real life)

As we have studied about  c++ classes and we found that we use  c++ classes to instantiate objects from it. I know you have a question in mind that what are objects as we are continually using this term. So it is the right time to answer your query.

We can define object as, anything which have some attributes , behaviors and also have a distinct name. Objects are the instantiation of the class.
 As while studying about  c++ class I have given you the example that   a map of  a building is a class because it do not have any physical existence but as contrary to it the building when build will be called an object ,because it has physical existence and if it has physical existence then it have attributes and definite behavior.

Let’s us take another example of object .Consider a  c++ class vehicle which has following attributes and behavior.

Attributes

(1)  Color
(2) Regno
(3) Price
(4) Modal

 Behavior

(1) Brake
(2) Accelerate
(3) Stop
(4) Reverse
 (5) Move

 Now as we know instantiation of class is called object, so we instantiate following objects from  class vehicle

(1) Car
(2) Bus
(3) Truck
(4) Bike

    Now Car, Bus, Truck and Bike are the objects which we instantiate from class Vehicle. As you can see that all the objects have definite name, have definite attributes also they contain definite behaviors. So they fulfill the requirements to be call an object.

  How To Declare Objects?


Objects are declared as class name followed by object name. suppose that our class is vehicle and our objects are Truck, Bus etc.

We can instantiate objects from class Vehicle as

Vehicle truck;
Vehicle  Bus;
Vehicle Car;
Vehicle Bike;

This is how we can write the objects. Now using these objects we can invoke different member functions to manipulate the states of the object.

Note::


          If you have further queries about “concept of objects” you can email me at jawad1647@gmail.com, I will try to solve all your queries and also please don’t hesitate to report mistakes as you know "to err is a human". You can report the errors by sending me a mail at the email address given above.

Concept of C++ classes And examples from real life

Till today we have studied that what is object oriented programming and what makes it useful as compared to procedural c++ programming.

Now it’s time to move on to the next level. We will first define that what is class?
As we have come across   the word class in our previous tutorials. So instead wasting more time, I think it is the appropriate time to explain the importance and definition of class.

A class is just a template, blue print or a carbon copy which do not have any physical existence. Physical existence means that it does not occupy a single byte of memory. You can also relate a class to a plan which does not have any physical existence. You can relate it with a map of a building. As we know that map has only architect of building but building do not actually exist. Map is just a plan or a template which tell us that building when build  should look like that.

A class is a mechanism using which we can instantiate our objects. Common things from objects are placed in a class and then each object is instantiated from it. We will discuss in details about objects after completing class section.

A class contain attributes which are also called data member or fields and behaviors which are also called method,member function or services of the class. Behaviors provided  in the class acts on the attributes and manipulate its value or state.

  Let’s take some examples from our real life to explain the concept of class.

Consider the example of Animal, animal is a class and we can drive  or instantiate objects from this class.We can drive following objects from the
class animal:
   (1) Man
   (2) Dog
   (3) Cat
   (4) Monkey
   (5) Eagle
       
These are the objects which we instantiate now we will put common attributes and behavior in our class animal and will instantiate objects from the class animal. It is important to note that we can also make our objects like human and monkey etc. a class and can instantiate objects from it but
It depends on requirement or problem statement.

Now take another example to further elaborate  the concept of classes in c++ programming.
Consider a class vehicle, from vehicle class we can instantiate many objects like

(1) Car
(2) Bus
(3) Truck
(4) Bike

Now these objects have some attributes and behavior consider following common attributes and behaviors of these objects:

(1) Regno
(2) Modal
(3) Color
(4) Price

 Behaviors

 (1) Drive
  (2) Stop
  (3) Accelerate
  (4) Brake


These all attributes and behavior are placed with in a class body. Now here object oriented programming in c++ comes to work, now we have only to instantiate object and our object will have all these attributes and behavior. It may feel strange to you that how to instantiate object from a
class. Do not worry about  that we will explain it latter. The basic purpose of this section is to let you know the basic purpose of class and give you understanding of what actually a class is, what it contains and how it is the best use of object oriented programming. I have explained all this by taking examples from real life.

Now if you have further queries about class you can email me at jawad1647@gmail.com, I will try to solve all your queries and also please don’t hesitate to report mistakes as you know to "err is a human". You can report the errors by sending me a mail at the email address given above.


Popular Posts