Tuesday, December 9, 2008

Abstraction VS Encapsulation

Here is some interesting info from http://www.codeguru.com/forum/archive/index.php/t-439501.html:

Abstraction is "selective ignorance". You choose to ignore the details of implementation be it in context of exposing interfaces/abstract classes in OOP or just plain functions or files or whatever that helps you categorize various components into independent entities that can take up some responsibility to add up to provide the complete behaviour expected of a system. It is above taking up a responsibility by a module / component / class / structure/function in itself and ignoring the details of how it will be actually implemented or implementation of any relationship/dependencies with other entities. Just that component and the functionality that it would provide.

In technology, when designing applications, it is not always possible that you think that way. The thinking becomes dirty by worrying about too many details all at the same time which might be needed but not for the specific task you want to accomplish. This is all part of the divide and rule policy that helps divide a particular task into contributing components and integrating them to build up the whole thing. That is what is abstraction.

Not really about hiding the implementation details but ignoring them. The hiding is what is called encapsulation. In JAVA using private/protected/public/internal/default access specifiers. Encapsulation is about hiding unncessary details that might change or might cause strong coupling between entities or the details which are not really required by other components to achieve what they take ownership of; which is never found to be beneficial.

Also find some useful content from http://stackoverflow.com/questions/24626?sort=newest:

Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object... encapsulation focuses upon the implementation that gives rise to this behavior... encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its essential characteristics

1 comment:

Vivek Athalye said...

couldn't understand much...

as per my understanding both the concepts are different and can not be / should not be compared ;)

btw... does anyone know what is internal access specifier... :-s .. i'm not in touch with java for last 3 yrs... but i don't think there was anything in java 1.4 like "internal"! can some1 clarify plz?