Encapsulation (computer programming): Difference between revisions

Content deleted Content added
No edit summary
→Cite book, web, harvnb
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 |edition=2, |publisher=Morgan Kaufmann, |year=2006, {{ISBN|isbn=0-12-633951-1}}, p. |page=481: "|quote=Encapsulation mechanisms enable the programme<sup></sup>r 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 2,|edition=2nd |publisher=Jones & Bartlett Publishers, |year=2007, {{ISBN|isbn=0-7637-3402-0 |page=396}}, p. 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|isbn=0-521-78098-5}}, p.|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}} p. |page=266 |ref=harv}}</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 |first=Wm. Paul |last=Rogers, [|url=http://www.javaworld.com/javaworld/jw-05-2001/jw-0518-encapsulation.html?page=9 ''|title=Encapsulation is not information hiding''], |publisher=JavaWorld.com, 05/|date=18/01 May 2001}}</ref><ref>{{cite book |first=Thomas M. |last=Connolly, |first2=Carolyn E. |last2=Begg, ''|title=Database systems: a practical approach to design, implementation, and management'', Edition|edition= 4,4th |publisher=Pearson Education, |year=2005, {{ISBN|isbn=0-321-21025-5}}, Chapter|chapter=Ch. 25,: "Introduction to Object DMBS", section§ "Object-oriented concepts", p. |page=814}}</ref>
 
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 which provide [[Closure (computer programming)|lexical closures]] view encapsulation as a feature of the language [[orthogonal#Computer science|orthogonal]] to object orientation.
Line 158:
== General definition ==
 
In general, encapsulation '''is one of the four fundamentals''' of OOP ([[object-oriented programming]]). Encapsulation refers to the bundling of data with the methods that operate on that data.<ref>{{cite web|lastname=Rodgers|first=Wm. Paul|title=Encapsulation is not information hiding|url=http://www.javaworld.com/article/2075271/core-java/encapsulation-is-not-information-hiding.html|publisher=JavaWorld|accessdate=2014-03-15}}<Rogers01/ref> Encapsulation is '''used to hide the values or state of a structured data object inside a class''', preventing unauthorized parties' direct access to them. Publicly accessible methods are generally provided in the class (so-called ''getters'' and ''setters'') to access the values, and other client classes call these methods to retrieve and modify the values within the object.
 
This mechanism is not unique to object-oriented programming. Implementations of [[abstract data types]], e.g. [[module (programming)|modules]], offer a similar form of encapsulation. This similarity stems from the fact that both notions rely on the same mathematical fundamental of an [[existential type]].<ref>{{harvnb|Pierce (|2002), Section|loc=§ 24.2 "Data Abstraction with Existentials"}}</ref>
 
== Encapsulation and Inheritance ==