Encapsulation (computer programming): Difference between revisions

Content deleted Content added
An information-hiding mechanism: Edited K.N. King reference
m ISBN: → ISBN using AWB
Line 1:
In [[programming languages]], <!-- [[object-oriented programming language]]--> <!-- and related fields, like [[OODMBS]],--> '''encapsulation''' is used to refer to one of two related but distinct notions, and sometimes to the combination<ref>{{cite book |first=Michael Lee |last=Scott |title=Programming language pragmatics |edition=2 |publisher=Morgan Kaufmann |year=2006 |isbn=0-12-633951-1 |page=481 |quote=Encapsulation mechanisms enable the programme<sup></sup>rprogrammer 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 |first=Nell B. |last=Dale |first2=Chip |last2=Weems |title=Programming and problem solving with Java |edition=2nd |publisher=Jones & Bartlett |year=2007 |isbn=0-7637-3402-0 |page=396}}</ref> thereof:
 
* A language mechanism for restricting direct access to some of the [[object (computer science)|object]]'s components.<ref>{{cite book |authorlink=John C. Mitchell |first=John C. |last=Mitchell |title=Concepts in programming languages |publisher=Cambridge University Press |year=2003 |isbn=0-521-78098-5 |page=522}}</ref><ref name=Pierce>{{cite book |last=Pierce |first=Benjamin |authorlink=Benjamin C. Pierce |title=[[Types and Programming Languages]] |publisher=MIT Press |year=2002 |isbn=0-262-16209-1 |page=266 |ref=harv}}</ref>
Line 22:
Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state. A supposed benefit of encapsulation is that it can reduce system complexity, and thus increase [[robustness (computer science)|robustness]], by allowing the developer to limit the inter-dependencies between software components{{Citation needed|date=April 2014}}.
 
Almost always, there is a way to override such protection &ndash; usually via [[Reflection_Reflection (computer_programmingcomputer programming)|reflection]] API (Ruby, Java, C#, etc.), sometimes by mechanism like [[name mangling]] ([[Python (programming language)|Python]]), or special keyword usage like <code>friend</code> in C++.
 
Below is an example in [[C Sharp (programming language)|C#]] that shows how access to a data field can be restricted through the use of a <code>private</code> keyword:
Line 141:
// which is the default behavior even without the keyword extern
</source>
Note on extern keyword from K.N. King.<ref>King, Kim N. C programming: a modern approach. WW Norton & Company, 2008. Ch. 18, p. 464, {{ISBN:|0393979504}}</ref>.
 
Clients call the API functions to allocate, operate on, and deallocate objects of an [[opaque data type]]. The contents of this type are known and accessible only to the implementation of the API functions; clients cannot directly access its contents. The source code for these functions defines the actual contents of the structure: