Content deleted Content added
Noted a key exception to an "almost always" statement. |
Citation bot (talk | contribs) Alter: url. URLs might have been anonymized. Add: archive-date, archive-url, authors 1-1. Removed parameters. Some additions/deletions were parameter name changes. | Use this bot. Report bugs. | Suggested by Josve05a | Linked from User:Josve05a/cite/wayback | #UCB_webform_linked 288/1938 |
||
Line 1:
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=
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>
== Meaning ==
In [[object-oriented programming languages]], and other related fields, <!-- [[object-oriented programming language]]--> <!-- and related fields, like [[OODMBS]],--> encapsulation refers to one of two related but distinct notions, and sometimes to the combination thereof:<ref>{{cite book |first=Michael Lee |last=Scott |title=Programming language pragmatics |edition= 2 |publisher=Morgan Kaufmann |year=2006 |isbn=978-0-12-633951-2 |page=481 |quote=Encapsulation mechanisms enable the programmer to group data and the subroutines that operate on them together in one place, and to hide irrelevant details from the users of an abstraction}}</ref><ref name="Dale">{{cite book |
* A language mechanism for restricting direct access to some of the [[object (computer science)|object]]'s components.<ref>{{cite book |author-link=John C. Mitchell |first=John C. |last=Mitchell |title=Concepts in programming languages |publisher=Cambridge University Press |year=2003 |isbn=978-0-521-78098-8 |page=522}}</ref><ref name=Pierce>{{cite book |last=Pierce |first=Benjamin |author-link=Benjamin C. Pierce |title=Types and Programming Languages |publisher=MIT Press |year=2002 |isbn=978-0-262-16209-8 |page=266 |title-link=Types and Programming Languages }}</ref>
* A language construct that facilitates the bundling of data with the [[method (computer programming)|method]]s <!-- I object to the word method being used here because in lisp languages programmers cannot distinguish methods from normal functions at the [[call site]]. Behavior equivalent to methods may be implemented as normal functions. encapsulation is not dependent on implementation but on behavior--> (or other functions) operating on that data.<ref name=Rogers01>{{cite web |last1=Rogers |first1=Wm. Paul |date=2001-05-18 |df=dmy |url=https://www.infoworld.com/article/2075271/encapsulation-is-not-information-hiding.html |title=Encapsulation is not information hiding |work=[[JavaWorld]] |access-date=2020-07-20}}</ref><ref>{{cite book |
Some programming language researchers and academics use the first meaning alone or in combination with the second as a distinguishing feature of [[object-oriented programming]], while some programming languages that provide [[closure (computer programming)|lexical closures]] view encapsulation as a feature of the language [[orthogonality#computer science|orthogonal]] to object orientation.
|