Field encapsulation

Field encapsulation

In computer programming, field encapsulation, also called data hiding, involves providing methods that can be used to read/write to/from the field rather than accessing the field directly. Sometimes these accessor methods are called "getX" and "setX" (where X is the field's name), which are also known as mutator methods. Usually the accessor methods have public visibility while the field being encapsulated is given private visibility - this allows a programmer to restrict what actions another user of the code can perform. Compare the following Java class in which the "name" field has NOT been encapsulated:

public class NormalFieldClass { public String name; public static void main(String [] args) { NormalFieldClass example1 = new NormalFieldClass(); example1.name = "myName"; System.out.println("My name is " + example1.name); } }

with the same example using encapsulation:

public class EncapsulatedFieldClass { private String name; public String getName() { return name; } public void setName(String newName) { name = newName; } public static void main(String [] args) { EncapsulatedFieldClass example1 = new EncapsulatedFieldClass(); example1.setName("myName"); System.out.println("My name is " + example1.getName()); } }

In the first example a user is free to use the public "name" variable however they see fit - in the second however the writer of the class retains control over how the private "name" variable is read and written by only permitting access to the field via its "getName" and "setName" methods.

Advantages

*The internal storage format of the data is hidden; in the example, an expectation of the use of restricted character sets could allow data compression through recoding (e.g., of eight bit characters to a six bit code). An attempt to encode characters out of the range of the expected data could then be handled by casting an error in the "set" routine.
*In general, the "get" and "set" methods may be produced in two versions - an efficient method that assumes that the caller is delivering appropriate data and that the data has been stored properly, and a debugging version that while slower, performs validity checks on data received and delivered. Such detection is useful when routines (calling or called) or internal storage formats are newly created or modified.
*The location of the stored data within larger structures may be hidden and so enabling changes to be made to this storage without the necessity of changing the code that references the data. This also reduces the likelihood of unexpected side effects from such changes. This is especially advantageous when the accessors are part of an operating system (OS), a case where the calling (application) code may not be available to the developers of the OS.

Disadvantages

*Access to a subroutine involves additional overhead not present when data is accessed directly. While this is becoming of less concern with the wide availability of fast general-purpose processors it may remain important in coding some real-time computing systems and systems using relatively slow and simple embedded processors.
**In some languages, like C++, the getter / setter methods are usually inline functions, so that when inlining is performed, the code looks just like direct field accessing, so it does not have the overhead.


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Generic Routing Encapsulation — (GRE) is a tunneling protocol designed to encapsulate a wide variety of network layer packets inside IP tunneling packets. The original packet is the payload for the final packet. The protocol is used on the Internet to secure virtual private… …   Wikipedia

  • Generic Stream Encapsulation — Internet protocol suite Application layer BGP DHCP DNS FTP HTTP …   Wikipedia

  • Multiprotocol Encapsulation over ATM — is specified in RFC 2684. It defines two mechanisms for identifying the protocol carried in ATM Adaptation Layer 5 (AAL5) frames. The AAL5 trailer does not include a type field. Thus, an AAL5 frame is not self identifying. This means that either… …   Wikipedia

  • Organizationally unique identifier — An Organizationally Unique Identifier (OUI) is a 24 bit number that is purchased from the Institute of Electrical and Electronics Engineers, Incorporated (IEEE) Registration Authority. This identifier uniquely identifies a vendor, manufacturer,… …   Wikipedia

  • Organizationally Unique Identifier — An Organizationally Unique Identifier (OUI) is a 24 bit number that is purchased from the Institute of Electrical and Electronics Engineers, Incorporated (IEEE) Registration Authority. This identifier uniquely identifies a vendor, manufacturer,… …   Wikipedia

  • Point-to-Point Protocol — Internet protocol suite Application layer BGP DHCP DNS FTP HTTP …   Wikipedia

  • Ethernet frame — A data packet on an Ethernet link is called an Ethernet frame. A frame begins with Preamble and Start Frame Delimiter. Following which, each Ethernet frame continues with an Ethernet header featuring destination and source MAC addresses. The… …   Wikipedia

  • Comparison of C Sharp and Java — The correct title of this article is Comparison of C# and Java. The substitution or omission of the # sign is because of technical restrictions. Programming language comparisons General comparison Basic syntax Basic instructions …   Wikipedia

  • Microelectromechanical system oscillator — Microelectromechanical system (MEMS) oscillators are timing devices that generate highly stable reference frequencies. These reference frequencies are used to sequence electronic systems, manage data transfer, define radio frequencies, and… …   Wikipedia

  • ATM Adaptation Layer 5 — (AAL5) is used to send variable length packets up to 65,535 octets in size across an Asynchronous Transfer Mode (ATM) network.Unlike most network frames, which place control information in the header, AAL5 places control information in an 8 octet …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”