Tuesday 16 July 2013

Distinguish between Data Abstraction and Data Encapsulation?

Data Abstraction:-Data abstraction means get the desired things.In data abstraction we use the features which we want ,without worry about the implementation.We have not the information how it will be done?Lets have an of real world example
You usually use mobile phone,when you do a call to your friend.Which steps you take (1)Type the number(2)Press the call button.


But you do not know what happens after that how the mobile implement this task to connect you with your friend..So the mobile has only abstract operation only which are type the number and press the call button.

Same as this example we have function of a object in programming, which done the particular task, these functions will do the task in there internal code only without showing it publicly.

Dummy code example:-
You might listen about the Stack which work on the principle to LIFO(Last In First Out).Stack have two operation PUSH and POP.So this function will do the data abstraction means these will do the internal work without telling the internal implementation.User will just use these two functions to do the task.
class Stack
{
       int numbers[]=new number[10];
       int top;
      Stack()
      {
         top=-1;
       }
      public void PUSH(int num)
      {
        ------------------
       }
       int void POP()
       {
        ------------------
        }

     public static void main(String ar[])
     {
             Stack marks=new Stack();
              marks.PUSH(10);
              marks.PUSH(20);
              int get=marks.POP();
           
}
\Note:-this Example is given to understand the concept only it will not be complied by the compiler.Please do not worry we will do this in proper format when you will have the knowledge about syntax etc in coming posts.



Data Encapsulation:-Data Encapsulation means wrapping the data and methods in a single unit.In programming class do the Encapsulation.Like capsule wrap up the medicine in it.

Example:-

class Chair
{
             int height;
             String color;
              Char()
              {
                 
                }
                public void setColor(String col)
                {
                           color=col;
                }
                ------
                ----------
}

So the Chair class is wrapping up the data members and methods in it

     






1 comment: