Encapsulation (computer programming): Difference between revisions

Content deleted Content added
Undid revision 1099539136 by 46.238.28.74 (talk)
Undid revision 1097725248 by Telesik1 (talk)
Tags: Undo Reverted
Line 1:
{{Short description|Bundling of data}}
In [[object-oriented programming]] (OOP), '''encapsulation''' refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components.<ref name="Rogers01" /> Encapsulation is used to hide the values or state of a structured data object inside a [[Class (computer programming)|class]], preventing direct access to them by clients in a way that could expose hidden implementation details or violate state invariance maintained by the methods.
 
Publicly accessible methods are generally provided in the class to access or modify the state more abstractly. In practice sometimes methods (so-called [[Mutator method|"getters" and "setters"]]) are provided to access the values indirectly, but, although not necessarily a violation of abstract encapsulation, they are often considered a sign-post of potentially poor object-oriented programming (OOP) design practice <ref>{{cite web |url=https://www.infoworld.com/article/2073723/why-getter-and-setter-methods-are-evil.html |title=Why Getter and Setter methods are evil |last=Holub |first=Allen |publisher=JavaWorld |date=September 5, 2003 |website=Info World |archive-url=https://web.archive.org/web/20200729073721/https://www.infoworld.com/article/2073723/why-getter-and-setter-methods-are-evil.html |access-date=January 17, 2021|archive-date=2020-07-29 }}</ref> (an [[Anti-pattern]]).
 
This mechanism is not unique to OOP. Implementations of [[abstract data types]], e.g., [[module (programming)|modules]], offer a similar form of encapsulation. The similarity has been explained by programming language theorists in terms of [[existential types]].<ref>{{harvnb|Pierce|2002|loc=§ 24.2 Data Abstraction with Existentials}}</ref>