Jump to content

Encapsulation (computer science)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 87.216.18.52 (talk) at 07:45, 12 March 2009. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer science, Encapsulation is the hiding of the internal mechanisms and data structures of a software component behind a defined interface, in such a way that users of the component (other pieces of software) only need to know what the component does, and cannot make themselves dependent on the details of how it does it. The purpose is to achieve potential for change: the internal mechanisms of the component can be improved without impact on other components, or the component can be replaced with a different one that supports the same public interface.

Encapsulation also protects the integrity of the component, by preventing users from setting the internal data of the component into an invalid or inconsistent state.

Another benefit of encapsulation is that it reduces system complexity and thus increases robustness, by limiting the interdependencies between software components.

The concept of encapsulation is most commonly associated with object-oriented programming, where the interface to an object is defined by its public methods, while its internal state is represented by private data. However, the idea is more general than this: for example, a relational database is encapsulated in the sense that its only public interface is the SQL language, which hides all the internal machinery and data structures of the database management system. As such, encapsulation is a core principle of good software architecture, at every level of granularity.

Encapsulating software behind an interface allows the construction of objects that mimic the behavior and interactions of objects in the real world. For example, a simple digital alarm clock is a real-world object that a lay person can use and understand. They can understand what the alarm clock does, and how to use it through the provided interface (buttons and screen), without having to understand every part inside of the clock. Similarly, if you replaced the clock with a different model, the lay person could continue to use it in the same way, provided that the interface works the same.