What is Abstraction?

E.g. If I say “chair” you know what I mean. I did not say it is large or smaller even it is a wooden or plastic chair. Without specifying what it is, you could able to understand the idea of the table (The Abstraction of the table).

Abstraction means we focus on essential quality of something rather than one specific example. It ignores the unimportant and irrelevant things. (e.g.  the mental Chair can have potential height and width. Unlikely to have a flavor because of it is irrelevant to the idea of the Chair.)

In object oriented language we don’t create separate class for Alex bank account and Bob bank account. Instead we create one class called bank account class for both. Because of we focus on the essential quality of the idea.

E.g. Both of Alex and Bob bank account has Account number and balance. But we discard irrelevant and unimportant things. We don’t focus on what is bank account class look like. We focus on what should bank account class look like under the particular circumstance on particular application, only focus on the essentials. There was a particular date the bank account has opened. But application doesn’t care about. Therefor we don’t need to define that attribute (opened date) in our bank account class.

Abstraction supports other fundamental of object orientation such as Inheritance and Polymorphism.

 What is Encapsulation?

“The idea of surrounding something”.  But it is also protects the content. In object orientation Attributes and behaviors are bundling into (encapsulate) same unite called class. But we want to restrict the access to the inner working of that class, any object base on that class. This is called the principle of data hiding or information hiding. Object should not reveal anything about itself except what is absolutely necessary for the other part of the application to work. (e.g. If we want to change the account balance of the account class objects.) We cannot change directly the balance without going through deposit and withdraw behaviors.  We can hide that data (balance). We can control accessible to it. Only accessible, inside that object itself. We can use deposit and withdraw method from the other part of the application and they can change the balance. But they can’t directly change the balance outside of the object.  This is referring to the idea of black boxing. We close inner work of the object and open few pieces to public. (E.g. we don’t need to know about the inner working of the telephone to make a call. It is enough to know about the using key pad ).

Why do we hide codes (data)?

To reduce the dependencies between different part of the application.
It is better to hide (encapsulate) every attributes and behaviors only expose absolutely necessary to expose.