Define Data Encapsulation?

Data Encapsulation combines data and functions into a single unit called Class. When using Data Encapsulation, data is not accessed directly; it is only accessible through the functions present inside the class. Data Encapsulation enables the important concept of data hiding possible.
Encapsulation, also known as data hiding, is an important object-oriented programming concept. It is the act of concealing the functionality of a class so that the internal operations are hidden, and irrelevant, to the programmer. With correct encapsulation, the developer does not need to understand how the class actually operates in order to communicate with it via its publicly available methods and properties; known as its public interface.
Encapsulation is essential to creating maintainable object-oriented programs. When the interaction with an object uses only the publicly available interface of methods and properties, the class of the object becomes a correctly isolated unit. This unit can then be replaced independently to fix bugs, to change internal behavior or to improve functionality or performance.
In the car analogy this is similar to replacing a headlight bulb. As long as we choose the correct bulb size and connection (the public interface), it will work in the car. It does not matter if the manufacturer has changed or the internal workings of the bulb differ from the original. It may even offer an improvement in brightness!

Tagged . Bookmark the permalink.

Leave a Reply