Defining Encapsulation

n 1: the condition of being enclosed (as in a capsule); "the encapsulation of tendons in membranous sheaths" 2: the process of enclosing (as in a capsule)


Encapsulation, one of the major Object Orientation traits (others being polymorphism and inheritance; abstraction and encapsulation and complementry concepts) is usually a topic of discussion during our technical interviews and/or general techno-babble. IMHO OO understanding draws a line between a coder vs. developer or a hacker who loves to write quality code for the enterprise and therefore we like to take it pretty serious.


I hold an strong argument against calling encapsulation interchangeable and synonym to data hiding or saying that ability to have private members enables encapsulation. I believe its more about how the physical or abstract entities of real life are represented and cacooned in form of objects defines it better or like the following defintion;


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. (1998-09-07)
Source: The Free On-line Dictionary of Computing, © 1993-2005 Denis Howe


or as institut fur informatik freiburg glossary states


The localization of knowledge within a module. Because objects encapsulate data and implementation, the user of an object can view the object as a black box that provides services. Instance variables and methods can be added, deleted, or changed, but as long as the services provided by the object remain the same, code that uses the object can continue to use it without being rewritten.


I think this idea of information “hiding“ came out of Grady Booch's statement about encapsulation:


We define encapsulation as follows (p46): “Encapsulation is the process of hiding all of the details of an object that do not contribute to its essential characteristics. “


and further


p45 The meaning of encapsulation. Abstraction and encapsulation are complementary concepts. Objects at a higher level of abstraction are shielded from lower-level implementation details. Encapsulation is also known as "information hiding" -- it prevents clients from seeing its inside view


Now this idea is being implemented in OO compliant languages by properties, readonly attributes, class instances (objects) and different level of members accessibility (public, protected, private, friend) but i won't call it “information hiding“ but more like defining the accessibility of the entity abstracted in code.


One of the cool new features of C# 2.0 we always hear about in presentations is providing different access level modifiers for properties, like follows.


public class Foo
{
    private string _name;
    public string Name
    {
        get { return _name; }
        protected set { _name = value; }
    }
}


Pretty neat, huh!


JavaWorld Wm. Paul Rogers Encapsulation is not information hiding


Nat Pryce, Mistaeks I Hav Made Encapsulation is not Information hiding


Object-Oriented Analysis and Design with Applications (2nd Edition) (Hardcover)
by Grady Booch


and the quote i liked on Pryce's website


Good judgement is the result of experience ... Experience is the result of bad judgement. — Fred Brooks

Share

2 thoughts on “Defining Encapsulation

Comments are closed.