Question

This might be the duplicate question but I haven't found the answer yet.

Link 1

Encapsulation:

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding

Link 2

Encapsulation:

"It […] refers to building a capsule, in the case a conceptual barrier, around some collection of things." — [Wirfs-Brock et al, 1990]

"As a process, encapsulation means the act of enclosing one or more items within a […] container. Encapsulation, as an entity, refers to a package or an enclosure that holds (contains, encloses) one or more items."

"If encapsulation was 'the same thing as information hiding,' then one might make the argument that 'everything that was encapsulated was also hidden.' This is not obviously not true."

Which one should I go with ? Or have I misunderstood the definition ?

Was it helpful?

Solution

The main point is that it doesn't really matter. Anyone can define a term in a slightly different way, and usually various authors adapt the meaning to the various contexts within which they use those terms.

You will not gain any enlightenment from trying to figure out which one is "right" and which one is "wrong". Quotes taken out of context are especially uninformative.

The important thing is to understand the underlying ideas without reference to the vocabulary items used to refer to them.

OTHER TIPS

There is disagreement as to whether the definition of encapsulation should include data hiding so this is going to be a strictly opinion answer. I believe that the latter definitions are more correct since data hiding is not unique to OO programming. It is a separate feature that does not preclude encapsulation which is the binding of functions/methods with a set of variables. In fact, data hiding was the hallmark of early modular programming in languages such as C and Pascal.

The first definition is very Java-centric. The second one is more generic. Both are correct. As to which one to go with, that's a subjective question. Since both are correct, I'd suggest going with the one you prefer...

Encapsulation is more than just data-hiding. It is decoupling internal data representation and implementation from the public interface. Thanks to encapsulation, as long as you don't break the interface contract, you can change internal implementation without anyone outside ever knowing. So I'd say encapsulation = data-hiding + implementation-hiding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top