Encapsulation (computer programming): Difference between revisions

Content deleted Content added
Add references and citations separately from each other. Add in-line citation.
Fix sfn issues.
Line 37:
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 from view outside of the object's definition. Typically, only the object's own methods can directly inspect or manipulate its fields. 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 interdependencies between software components.{{Citation needed|date=April 2014}}
 
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 §154 Item15 Minimize the accessibility of classes and members|pp=73-77}}) offer the programmer a degree of control over what is hidden, typically via keywords like <code>public</code> and <code>private</code>.<ref name=Pierce/> ISO C++ standard refers to <code>protected</code>, <code>private</code> and <code>public</code> as "[[access specifiers]]" and that they do not "hide any information". Information hiding is accomplished by furnishing a compiled version of the source code that is interfaced via a header file.
 
Almost always, there is a way to override such protection &ndash; 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.