RSS

OOP Concepts – Encapsulation !!

02 May

Overview !!

All applications manipulate data. Many older legacy applications built by using programming languages that are not object-oriented, such as C and COBOL, typically separate the code that processes data from the code that manages and stores this data. Data management code was frequently provided in the form of code libraries, and the data-processing elements of an application were frequently built into the business logic of the application. Several applications that implement different parts of a business system might all use the same data that they access through a code library, but perform their own operations on that data.

However, in this scenario, if the format of the data changes, the code library that manages the data may also need to change. All of the applications that use the original code library may need to be refreshed to use the new version, and the logic in these applications may also need to change to handle the new structure of the data. Such changes can be difficult to perform (or even locate), and any applications that are not updated correctly may exhibit bugs and generate errors.

What Is Encapsulation?

Encapsulation is the ability of a type to hide its internal data and implementation details, making only specific parts of the type accessible to applications.

Encapsulation is an essential object-oriented principle. For example, when you define a class, you should always define the fields as private members so that external code cannot access the fields directly. The only way for external code to interact with an object or class is through a well-defined set of public methods and properties.

Note: Properties provide a structured way to expose the ability to retrieve and set the values of private fields. Properties are covered in a later module in this course.

Benefits of Encapsulation

Encapsulation enables you to hide information. When you hide information, such as the internal state and implementation details of a type, the external code focuses on only the useful characteristics of the object. For example, the internal mechanism of a telephone is hidden from users; the wires, switches, and other internal parts of a telephone are encapsulated by its cover and are inaccessible to users. You may not have any idea about how a telephone works internally, but you can still use it.
In addition, when you hide the internal state of a type, client applications cannot modify or corrupt this state; this prevents changes that can cause the type to malfunction and produce unexpected results.

By using encapsulation, you can also easily change the implementation details of your type; applications that use your type do not need to be rewritten. As long as the public methods and properties that a class exposes do not change, any existing client applications should still work correctly

 

You can see the following article “Objects and Methods Accessibility

 

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: