Design Concepts  «Prev  Next»
Lesson 3 Encapsulation
Objective Define encapsulation.

Encapsulation Information Hiding

Two Phases Object Definition
Encapsulation is sometimes also called information hiding because the implementation of an object is hidden. Unfortunately, this description ignores the more important aspect of encapsulation, which is what an object exposes.
To illustrate, given a set of devices such as a radio, a TV, and phone, what would you need to know about them to place a call? You are probably thinking, “I just need to know how to use the phone.” But you actually need to know two things about each device: its purpose and interface.

Expose the purpose and interface

First you need to know which device is designed for the purpose of making calls. Second, you need to know the interface of the device, that is, how to initiate a call, place a call, and terminate a call. For an object to be useful, that object has to expose both its purpose and its interface.
Some people say that the interface defines an object. Be careful. Many objects can share the same interface but have a different purpose. For example, a TV and a VCR share much of the same interfaces, such as volume controls and channel changing. But a TV cannot play tapes, and a VCR cannot display the contents of tapes. The two objects are designed for different purposes.

Object Solutions: Manage Object-Oriented Project

Hide the implementation

To use a phone, do you need to know how it transforms the mechanical press of a button into an electronic pulse? No. This part of the phone is hidden. The implementation of the phone’s interface and the data it uses for the implementation are hidden.

Interchangeable parts

Design based on encapsulation is a major key to creating reusable and interchangeable objects. Just as in physical objects, like disk drives or car engines, all you need to know is the purpose and the interface to determine whether one device may be used in place of another.

Hidden Exposed Object Aspects - Exercise

Click the Exercise link below to identify which aspects of two common objects are hidden or exposed.
Hidden Exposed Object Aspects - Exercise