James Ray Anderson

James Ray Anderson
James Ray Anderson
0 comments

Quick and Easy OOP - Part III - Encapsulation

8:00 PM
Overview
Before we get into the topic of Encapsulation, let me first define Encapsulate, as defined in Dictionary.com:

en-cap-su-late, verb
1.  to place in or as if in a capsule.
2.  to summarize or condense
That pretty much summarizes Encapsulation.  You are condensing and isolating a component, in this case an object. 

In programming, there are two types of Encapsulation:
  • Access Restriction
  • Bundling of data and operating on that data

Bundling of Data and Operating on Data
For example, you want an object that pulls data from a database.  However, you don't want every developer writing their own way of doing this.  So you encapsulate the functions of accessing the database and pulling data. 

Now, in order to use the data, you need to expose some properties and methods by which another object can interact.  You are controlling the interaction, controlling how the data is accessed and updated, while simplifying the overall process.

This is done by creating an outward facing set of method(s) or properties as well as other methods and variables that are not seen - but interact with the data or logic internally within the object.  that hide the manipulation or access of the underlying data or information.

The Benefits:
  • Protects the integrity of the data from users setting internal data
  • Reduces system complexity.  You only need to access what is needed rather than having to know all about accessing the data
Access Restriction
the access restriction side of encapsulation is what provides security and limits what interfaces, methods, or actions can be taken on an object.  For example, just because you have a Data object doesn't mean that you will make all of the internal methods public, such as creating a connection to the database.  You might want to keep that hidden.  That is done through language specific keywords such as public and private access specifiers.

The Benefits:
  • Protects the logic within the object from being access directly
  • Simplifies what users are allowed to access

0 comments:

 
Toggle Footer
Top