Encapsulation For Dummies

While ObjectOrientedForDummies is a good summary of OO basics, you should be aware that its use of "encapsulation" is different from common industry practice. Common industry practice uses the term "encapsulation" to mean "separating interface from implementation and managing data access."

Citations:

[The Wikipedia link above now redirects one to a page titled "Information Hiding" (http://en.wikipedia.org/wiki/Information_hiding). Somebody at Wikipedia does't agree that EncapsulationIsNotInformationHiding.]

In object-oriented programming the concept of encapsulation is used to create abstract datatypes that should be possible to modify only through their external interface. (http://www.lirmm.fr/~ardourel/WEAR/wear03Skoglund.pdf, "A Survey of the Usage of Encapsulation in Object-Oriented Programming", 22 July 2003).

Also known as INFORMATION HIDING. This is complementary to abstraction and is used to describe the process of defining the code and data which form an object. (Many programmers erroneously interpret this to mean data hiding. It isn't: we hide both data and code). Within the object, some data and code may be accessible directly as the interface of the object, while other parts of the object remain private. In fact it is usual for all data to be private, access to values being through the return of (public) member functions. (http://tutorials.beginners.co.uk/read/id/286, "C++ Programming - Object Oriented Paradigms")

"C++ provides three levels of privacy: public member functions, which effectively implement abstraction; private member functions and data, which implement the Encapsulation, and protected member functions and data, which are somewhere between the two, and are of more concern when considering INHERITANCE. There is also the Friend mechanism, where co-operating classes can have access to each other's private member functions and data." (ibid)

"Generally, therefore, it is a goal of encapsulation to ensure that all information and processes associated with an object are contained within its definition. It should not be possible to affect the state of an object other than through its public interface." (ibid)

The ability to provide users with a well-defined interface to a set of functions in a way which hides their internal workings. In object-oriented programming, the technique of keeping together data structures and the methods (procedures) which act on them. (http://www.hyperdictionary.com/dictionary/encapsulation)

The ability to provide users with a well-defined interface to a set of functions in a way which hides their internal workings. In object-oriented programming, the technique of keeping together data structures and the methods (procedures) which act on them. (FOLDOC, http://www.nightflight.com/foldoc-bin/foldoc.cgi?encapsulation)

Far from being an "official" definition, I think the "FOLDOC" use simply mistaken. For example, the FOLDOC summary of the history of object oriented programming (http://www.nightflight.com/foldoc-bin/foldoc.cgi?object-oriented+programming) jumps from SIMULA-67 to C++, while describing Smalltalk as "another popular object-oriented programming language" (though they do admit that it was "seminal").

Also called "information hiding", this ensures that objects cannot change the internal state of other objects in unexpected ways; only the object's own internal methods are allowed to access its state. Each type of object exposes an interface to other objects that specifies how other objects may interact with it. Some languages relax this, allowing some direct access to object internals in a controlled way, and limiting the degree of abstraction. ("Science Daily", http://www.sciencedaily.com/encyclopedia/Object_orientation)

I think that the term FOLDOC (and apparently therefore Eric) was intermingling with "encapsulation" is "Abstraction". Let me start an AbstractionForDummies page.


The other side:

It's possible to encapsulate (group) data structures with methods without hiding anything. Some languages have no visibility restrictions, yet they provide encapsulation. Keeping the terms encapsulation and data hiding distinct is useful, regardless of what some web sites say.

"Some web sites"? The meaning of both "encapsulate" and "abstraction" are clear, the definitions are consistent and have been for two decades. The starting place for changing their meaning, if you choose to attempt that, should surely be the community in which they've been used for twenty years, as opposed to explanatory text for newcomers. No wonder there's so much confusion here on Wiki about "object-oriented" software, theory, and practice if even these kinds of elementary concepts are mangled. Why stop with "encapsulate" and "abstraction", lets argue about the meaning of "bit", "byte", "true", "false", and so on.

I'm not trying to change the meaning of encapsulate. I'm trying to preserve the meaning I thought it always had. I think a lot of people are unclear about the distinction, but I don't think we should let go of it.

Maybe it's time from you to start contributing to wiki with the willingness to learn. So far, you've shown an excessive willingness to "preserve" what you know.

What good will come from changing "encapsulation" to mean "data hiding"?


Fans of historical data may care to take a look at the works of DavidParnas:

and this article which tries to clear up the confusion:


Factored out some of the threadmess.


The definition of encapsulate according to a dictionary is "enclose in a capsule or other small container". This can be done through modules, nested procedures, closures, local variables, static variables... or even just a parameter to direct a method or procedure to do something different based on what the parameter is set to (hiding the intricate details in the method or procedure, i.e., putting it in a capsule). Don't believe the OOP hype; apply encapsulation to your programming no matter what paradigm(s) you use.


See also: GateKeeper, EncapsulationDefinition


CategoryDummies, CategoryFacePalm


EditText of this page (last edited May 9, 2011) or FindPage with title or text search