Content deleted Content added
mNo edit summary |
m Streamlined text and corrected grammar |
||
Line 2:
In software systems, '''encapsulation''' refers to the bundling of data with the mechanisms or methods that operate on the data. It may also refer to the limiting of direct access to some of that data, such as an object's components.<ref name="Rogers01" /> Essentially, encapsulation prevents external code from being concerned with the internal workings of an object.
Encapsulation allows developers to present a consistent interface that is independent of its internal implementation. As one example, encapsulation can be used to hide the values or state of a structured data object inside a [[Class (computer programming)|class]]
All [[object-oriented programming]] (OOP) systems support encapsulation,<ref>{{Cite web |title=What is Object-Oriented Programming (OOP)? |url=https://www.techtarget.com/searchapparchitecture/definition/object-oriented-programming-OOP |access-date=2024-03-02 |website=App Architecture |language=en}}</ref><ref>{{Cite web |title=Encapsulation in Object Oriented Programming (OOPS) |url=https://www.enjoyalgorithms.com/blog/encapsulation-in-oops/ |access-date=2024-03-02 |website=www.enjoyalgorithms.com}}</ref> but encapsulation is not unique to OOP. Implementations of [[abstract data types]], [[module (programming)|modules]], and [[library (computing)|libraries]]
== Meaning ==
Line 16:
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.
The second definition
The features of encapsulation are supported using [[class (computer programming)|class]]es in most object-oriented languages, although other alternatives also exist.
Line 39:
== Information hiding ==
{{Main|Information hiding}}
Under the definition that encapsulation "can be used to hide data members and member functions", the internal representation of an [[object (computer science)|object]] is generally hidden
Some languages like [[Smalltalk]] and [[Ruby (programming language)|Ruby]] only allow access via object methods, but most others (e.g., [[C++]], [[C Sharp (programming language)|C#]], [[Delphi (programming language)|Delphi]] or [[Java (programming language)|Java]]{{sfn|Bloch|2018|loc=Chapter §4 Item 15 Minimize the accessibility of classes and members|pp=73-77}}) offer the programmer
Almost always, there is a way to override such protection – usually via [[Reflection (computer 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++. Systems that provide object-level [[capability-based security]] (adhering to the [[object-capability model]]) are an exception, and guarantee strong encapsulation.
|