Abstraction and Encapsulation

October 27, 2015 Posted by WithU Technologies
Abstraction only focus on essential qualities of something.

What is Abstraction?

·         Concept of providing only essential information.
·         Representing essential features without revealing the implementation details.
·         Fundamental building block of object oriented programming languages.


Example:

int main(){

      int a;

      cout<<"Enter an integer number "<<endl;

      cin>>a;     

}

·         As here we are not aware of the implementation of cout and cin objects, but we use them to print output and to take input respectively.

·         We have a concept, but it is separate from any specific instance.

·         For instance, we need to create multiple bank accounts. At that time abstraction help us to write a single bank account class first which will be the blue print for all other bank accounts.

Then we create multiple bank account object based on that class, rather than creating a separate class for each bank account which is also wastage of time and space too.          



 What is Encapsulation?

·         Encapsulation is the procedure of Data hiding.
·         Restricting access to the inner working of that class.
·         It only reveals those data which are required by application components to effectively run the program.

·         It controls the way data is accessed, modified or updated.

·         It is also known as “Black Boxing” which refers to close the inner working zones of object, except of the pieces that we want to make public.

·         Thus we can change attribute or implement new methods, without altering the overall program.

Example:





So the balance is attribute is kept hidden from rest of the application components and only deposit() and withdraw() behaviours can access them.

This way the balance will only change when the consumer deposit or withdraw money to/from the account.