Thursday, April 15, 2010

Abstraction vs Encapsulation

A simple definition of abstraction is to hide actual implementation of an object from the external world that would use the object and just exposing the functionality.
For example,
i. A program that is drawing circles and squares using those objects need not know how those objects are implemented. It is enough for the program to know what the behavior of these objects is, and how to use these objects (rather than how these objects are implemented internally).
ii. When you change the gear of your car, you know the gears will be changed without knowing how they are functioning internally. Abstraction focuses on the outside view of an object (i.e. the interface)
iii. Taking one another example of electric switch board in our house. Wires inside the switch board are encapsulated (hidden) and switches are like methods using which we can put-on/off the bulb/fan. And the process behind the switch-on/off event is abstracted, i.e. we can never know, how bulb/fan is working when we put-on the switch.
iv. The benefit is that you don't need to deal with the (potentially big and complicated) inner workings of the object in order to use it. So you are protecting the user from the object.
i. Combining the data(properties/attributes/information) and the methods (behaviors of class with the data) that can manipulate that data into one capsule (class/object) with guarantees that the encapsulated data is not accessed by any other function/method outside the encapsulated object and must follow the programmer's/ owner's strategy regarding manipulation of data members.
ii. In other words, Encapsulation means put the data and the function that operate on that data in a single unit (information hiding) which prevents clients from seeing it's inside view i.e. clients can view data by the interface given by the programmer by means of methods only.
iii. Such methods hold the behavior of the abstraction (i.e. hide the answer of how it works) is implemented.
iv. Encapsulation can be achieved by using proper access modifiers like, public, private, etc to control the scope of the variables.
v. Encapsulation promotes separation of state of an object from its behavior.Hence, we can say that when we encapsulate data and methods that operate on data into one object, the external program that uses this object need not know the internal workings of the object to use the object. Thus is making the object abstract data type to the external program.

Plz. click any one advertisement shown on this blog to manage this blog.


  1. Thanks for sharing information. by the way I have also shared my experience as What is abstraction in Java , Please let me know how do you find it.

    Difference between comparable and comparator in java

  2. sir, thank you very much for such beautiful example

  3. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai | Java Training Institutes

    Online Java Training Java Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai