Encapsulation

Data encapsulation is a technique that packages related data (properties) and procedures (methods) together in an object. It provides the ability to hide the details of the implementation of an object, while making the object’s functionality available to other objects. In other words, PSL enables you to wrap an object, its properties, and its methods into a self-contained unit without enabling outside source code to access the code or data within the unit. You know how to access the encapsulated code and can therefore use it regardless of the implementation details.

The concept of a class is the basis of encapsulation. A class defines the structure and behavior (i.e., data and code) shared by a set of objects. Each object, therefore, has the structure and behavior defined by its class. The portion of an object that is accessible to other types of objects is called the object’s interface. The interface for the class Telephone might consist of methods to make the telephone Ring or Call Forward. The implementation of that class includes code that implements the methods. The distinction between the interface and the implementation of a class is that a class may change, but as long as the interface remains the same, the changes do not precipitate changes to other objects that may use the class.

You can further encapsulate methods and variables in a class by identifying them as public or private. You can access public methods and variables outside the class in which you define them; however, you can only access private methods and variables from other methods within the same class.

image\cam.gif

Data Encapsulation