Jump to content

Data encapsulation: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m moved Private (Java) to Data encapsulation: Create generic article from overly specific article
Reverting edit(s) by 122.172.81.17 (talk) to rev. 1071847629 by NikhilaSundar: Unexplained content removal (RW 16.1)
 
(45 intermediate revisions by 38 users not shown)
Line 1: Line 1:
Data encapsulation, also known as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user. The user can only perform a restricted set of operations on the hidden members of the class by executing special functions commonly called ''methods'' to prevent attributes of objects from being easily viewed and accessed. '''Data encapsulation''' may refer to:
<!-- Please do not remove or change this AfD message until the issue is settled -->{{#ifeq:{{FULLPAGENAME}}|Special:Undelete| |{{#if:{{{nosubst|}}}|<div style="display:none;">}} {{#ifeq:{{NAMESPACE}}||{{#switch:{{NAMESPACE}}|= |#default={{error:wrong namespace}}<div style="display:none;">}}|{{error:not substituted|AFD}}<div style="display:none;">}}}} {{#if:{{{nosubst|}}}|</div></div>}}
<div class="boilerplate metadata" id="afd" style="margin: 0 5%; padding: 0 7px 0px 7px; background: #EDF1F1; border: 1px solid #999999; text-align: left; font-size:95%;">
'''This article is being considered for deletion in accordance with Wikipedia's [[Wikipedia:Deletion policy|deletion policy]][[Template:Afd|.]]'''<br />
You may share your thoughts on the matter at '''[[Wikipedia:Articles for deletion/{{{1|Private (Java)}}}|this article's entry]]''' on the [[Wikipedia:Articles for deletion|Articles for deletion]] page.<br />
Please improve the article if possible, but the article must not be blanked, and this notice must not be removed, until the discussion is closed. For more information, particularly on merging or moving the article during the discussion, read the [[Wikipedia:Guide to deletion|guide to deletion]].<br/>
----
<small>''[[Template:AfD footer|Steps to list an article for deletion]]: &#123;&#123;subst:afd&#125;&#125; • [http://en.wikipedia.org/enwiki/w/index.php?action=edit&preload=Template:Afd2+starter&editintro=Template:Afd3+starter&title=Wikipedia:Articles+for+deletion/{{PAGENAMEE}} Preloaded debate] ''OR'' &#123;&#123;subst:afd2|pg={{PAGENAME}}|cat=|text=}} &#126;&#126;&#126;&#126; • &#123;&#123;subst:afd3|pg={{PAGENAME}}}} [{{SERVER}}{{localurl:Wikipedia:Articles for deletion/Log/{{CURRENTYEAR}}_{{CURRENTMONTHNAMEGEN}}_{{CURRENTDAY}}|action=edit}} log]
</small></div>
{{#ifeq:{{NAMESPACE}}||{{#switch:{{NAMESPACE}}|= |#default=</div>}}|</div>}}
{{{category|[[Category:Articles for deletion]]}}}
<!-- End of AfD message, feel free to edit beyond this point -->
The [[Java (programming language)|Java programming language]] allows the programmer to encapsulate data. Encapsulation of data is done to protect the data. If you are writing a piece of software with delicate data, such as a length and width of a window, you can prefix a variable declaration with the keyword "private". The piece of data that has been declared private will only be viewable by methods and constructors from within the same class.


* The wrapping of private data in classes in object-oriented programming languages: see [[Encapsulation (object-oriented programming)]], [[information hiding]], [[separation of concerns]]
== Example ==
* The wrapping of network data by a lower layer in the [[OSI model]] into a single unit where a higher layer can extract the relevant data: see [[Encapsulation (networking)]]

{{Disambiguation}}
public class PrivateTest {
private int x = 10;
public int getX() {
return x;
}
}

The variable x can not be accessed outside of the class PrivateTest. In other words only an instance of PrivateTest may modify or look at the variable x. This is useful if x is sensitive. If you have written an API that does graphics you can prevent some application programmer from modifying your variable from his or her classes to help protect them from bad things. Imagine a program that created a thread to calculate velocity of a car. One method set the conditions that the velocity should be calculated under and one calculated the velocity. If an application programmer called the first method and then changed one of the conditions directly unexpected results may occur.

Java is a safe language that has built-in constructs to make it easy to protect the runtime environment from fatal crashes. The "private" statement is just another means to that end. It is not strictly necessary to write code that is properly encapsulated but it is a good habit and can make for considerably safer code.

{{compu-lang-stub}}

[[Category:Java programming language]]

Latest revision as of 14:28, 22 March 2023

Data encapsulation, also known as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user. The user can only perform a restricted set of operations on the hidden members of the class by executing special functions commonly called methods to prevent attributes of objects from being easily viewed and accessed. Data encapsulation may refer to: