User:Hpesoj00/sandbox/Encapsulation

In computer science, data encapsulation or simply encapsulation is the application of the principle of information hiding, whereby certain design information is hidden from the users of an abstraction.

Information hiding principle
The concept of "information hiding" was described in 1971 by David Parnas, who proposed that access to information should be restricted by design in order to reduce the interconnectedness of a system. He described three apparently conflicting design concerns: the desire to have a user-friendly external interface, the desire to reduce development time by splitting a system into modules which can be designed independently, and the desire to easily change the implementation details of a system; he suggested that these concerns could more easily be reconciled if design information were not accessible to everyone. He proposed that programmers should not be responsible for deciding which information to use, but instead system designers should restrict the information that is available to programmers. In the appendix to the article, he gave an example of how to document a module with a formal interface definition so as to hide implementation details from the user.

In 1972, Parnas showed how information hiding can be used as a design criterion for dividing a system into modules. By defining the interfaces between modules such that they hide design decisions from each other, he suggested that a system can support a wider range of functionality without having to change the interface definitions. He noted that care must be taken to ensure that decomposing a system in this way does not negatively impact efficiency.

Data encapsulation
The term "encapsulation", or more specifically "procedural encapsulation", was used in 1973 by Stephen Zilles to describe how procedures can be used to encapsulate or "isolate" the underlying representation of a data type. He noted that by using encapsulation: In the same year, Barbara Liskov and Zilles attempted to formalize the concept of an abstract data type in the context of what would become the programming language CLU, describing the concept of an object (which they also referred to as a "function cluster" or "cluster" for short) as the embodiment of "a data type being completely characterized by the operations on that type". They emphasised that external users view objects as indivisible while the objects may be internally composed of more primitive elements, and referred to abstract data types "encapsulat[ing]" their constituent components.
 * the operations defined on a type can be formalized to ensure the correctness of objects of that type;
 * modules have to make minimal assumptions about other modules to which they have access; and
 * encapsulated data is protected from both accidental and malicious modification.

In 1974, William Wulf outlined the details of the Alphard programming language. He cites the overabundance of "implicit abstractions" – implementation details such as data structures and control flow mechanisms – and the lack of "explicit abstractions" – meta-level abstractions such as procedures and macros – in programming languages at the time, as one of the motivations for his proposals. He describes how Alphard allows certain names to be selectively "exported" from an abstract data type (a "form" in Alphard parlance) so that they are available for use with instances of the type.

In 1974, Lawrence Flon described encapsulation as relating to the operations defined for a type, and as referring to "the collection of all information relevant to a particular implementation". He described three different categories a language may fall into regarding encapsulation: He described the issue of "exportation" – controlling which names are visible to users of a type – as relating to the concept of information hiding, noting that no existing languages yet supported exportation features. He also referred to the CLU and Alphard papers as "proposal[s] for encapsulation".
 * 1) operations are bound to a definition of a type;
 * 2) operations are bound to an instance of a type (an object); or
 * 3) operations are bound to neither a definition nor an instance of a type.

In 1975, Liskov used the term "encapsulation" to describe the definition of a type so that objects of that type may be manipulated using only specific programmer-defined operations, and the representation of an object is not visible outside of the definition of its type. She referenced CLU and Alphard as programming languages being developed with support for type-checking mechanisms for enforcing encapsulation. She described how encapsulating type definitions simplifies specifications by reducing the amount of information they contain, as well as improving the provability of a program in two regards: In the same year, Parnas discussed how attempting to deal with errors at the point where they occur violates the "information hiding principle"; he suggests that interfaces between modules should be designed to communicate information about errors, error handling, and assumptions.
 * 1) Assumptions can be made about the implementation of a type since its representation can only be modified via the defined operations.
 * 2) Code that uses the type can be reasoned about without having knowledge of the representation of the type.

In 1976, Theodore Linden described how abstract data types can be used to encapsulate or "protect" the data representation of an object by allowing direct access only through procedures which are defined as part of the same type. Linden suggested that encapsulation is important when: He noted that encapsulation using abstract data types can be used to enforce additional structure as well as to allow redundancy for error detection and recovery. He observed that abstract data types are preferable to procedures as a unit of modularity due to their ability to encapsulate non-global persistent state.
 * 1) "the data has a complex structure";
 * 2) "the data might get into an inconsistent state"; or
 * 3) "the data needs to be protected for reasons of privacy or security".

In the same year, William Wulf et al. described encapsulation as the separation of the implementation of an abstraction from its use in an abstract program; they described how the programming language Alphard used an abstraction mechanism to allow encapsulation through information hiding, by making all representational data inaccessible and allowing access only to properties defined in a formal specification.

In the same year, Parnas et al. described how programming languages that support abstract data types enable the programming methodology of information hiding.

In 1978, Wolfram Bartussek and Parnas described "encapsulation" and "data abstraction" as being synonymous with the "information hiding principle"; they describe encapsulation as the design of subtasks which hide their details from the rest of the system, and note that the interface description should define only the requirements that a component must satisfy, terming this the "specification". They suggest that to ensure trouble-free development, specifications should be verifiable for consistency and completeness. In a different paper in the same year, Parnas suggested that "encapsulation", "information hiding" or "abstraction" is intended to isolate abstract components of a system using interfaces which are insensitive to change, so that as far as is possible, the presence or absence of a component is hidden from other components. He noted that this is necessary because software lacks the generality of a mathematical proof, and must be tailored to the situation at hand.

Object-oriented programming
In 1980, the first implementation of the programming language C++ included public and private access control allowing data to be encapsulated within a class.

In 1982, Tim Rentsch referred to "intelligence encapsulation" – "view[ing] objects from outside to provide a natural metaphor of intrinsic behaviour" – as the first principle of object-oriented programming. He described object-oriented programming as being related to abstract data types, data encapsulation, information hiding and modularization, and noted that the programming languages Alphard, Ada and CLU are designed with a focus on data encapsulation.

In 1984, Albert Cohen looked at the concepts of data abstraction and data encapsulation in the context of object-oriented programming. He described both concepts as referring to the localization and concealment of implementation details, but drew a distinction saying that abstraction refers to classes, whereas encapsulation refers to objects. Cohen went on to state that object-oriented programs are those in which all data structures are encapsulated. Further, he suggests that the principle of information hiding states that it should not be necessary to consider, or possible to access, inessential details, and that encapsulation enforces this for objects.

In 1985, Mark Stefik and Daniel Bobrow discussed the distinction between "strict encapsulation", where a language restricts access to the instance variables of an object, and encapsulation by convention. They note that proponents of strict encapsulation argue that limiting knowledge of an object's internal representation keeps problems concerning the object's state well-bounded. They define encapsulations as the principle that "users of objects ought not have privileged access to the internals of those objects".

In 1986, Alan Snyder discussed the interaction between encapsulation and inheritance. He defines encapsulation as "a technique for minimizing interdependencies among separately-written modules by defining strict external interfaces", and suggests that a module is encapsulated if it can only be accessed via its external interface, and that programming languages support encapsulation by allowing minimal external interfaces to be defined and enforced so that a module can be modified without affecting its users. He states that encapsulation, while making programs easier to understand, maintain and modify – benefits which are especially important for large systems and long-lived data – is severely compromised by inheritance. Allowing subclasses access to the instance variables of a superclass compromises encapsulation. He also noted that languages could provide an additional level of visibility, since the interface designed for descendants of a class may not be suitable for users of instances of the class. He noted that the visibility of inheritance restricts modification of an inheritance hierarchy, and is thus an encapsulation issue, suggesting that it is important to allow inheritance without users of a class knowing about it. He further suggested that inheritance should not form part of the external interface of a class, and that a class should be considered a subtype of another class if it meets its external interface. Interfaces should be defined separately. He also noted that in C++ modules are encapsulated rather than individual objects, since objects of the same type can access each other's internals. In the same year, protected access control was added to C++, allowing separate interfaces to be defined for subclasses.

In 1987, Josephine Micallef described encapsulation as "the strict enforcement of information hiding". She listed three levels of encapsulation that can be provided by a programming language: the interface provided to instantiating clients, the interface provided to inheriting clients, and the visibility of inheritance. She noted that many languages at the time did not have good support for encapsulation with inheritance, as they did not allow parts of the interface to be exposed only to inheriting clients, and also did not distinguish between "logical" and "physical" inheritance hierarchies, meaning that inheritance of interface required inheritance of implementation. In the same year, Snyder suggested that object-oriented programming is "any form of programming that exploits encapsulation".

In 1988, Peter Sanderson and Lawrence Rose described encapsulation and inheritance as central to object orientation, and described encapsulation as "the modeling of system elements so that only the specification of their structure and behavior is visible to the modeler" noting that it contributes to modularity and data hiding. He said that encapsulation is provided through the class construct in C++. In the same year, Karl Lieberherr and Ian Holland encoded the idea of encapsulation in the Law of Demeter, which. They noted that application of the law makes software maintenance easier, especially when changes are made at a high rate, but that greater levels of encapsulation come at the cost of greater numbers of methods and arguments, speed of execution, and sometimes code readability.

In 1989, Rebecca Wirfs-Brock and Brian Wilkerson advocated and provided an approach for maximizing encapsulation, noting encapsulation as the main form of abstraction by which complexity is managed in object-oriented programming. They stated that the full benefit of object-oriented programming can only be achieved with encapsulation as a goal of the design process. They describe abstract data types as the "encapsulation of data and the algorithms that operate on that data". They describe encapsulation as the key to making software more resuable, refinable, testable, maintainable and extensible, and suggest that encapsulation can be increased by taking a responsibility-driven as opposed to a data-driven approach to object-oriented design, using a client-server model to focus on the responsibilities of each class rather than the structure of the system, allowing implementation decisions to be postponed to the implementation stage. In the same year, Malcolm Atkinson et al. discussed encapsulation in the context of object-oriented database systems, and stated that encapsulation addresses the need to cleanly separate specification and implementation, as well as the need for modularity in order to aid application structure and for protection and authorization. They described encapsulation as coming from the idea of abstract data types, where the interface is the visible part of an object and the implementation comprises the data and procedures which operate on that data. They noted that in a relational database system, the separation of interface from implementation is not as clear cut, but that encapsulation can be achieved if database records are manipulated using operations that are also stored in the database.

In 1990, Peter Wegner defined encapsulation as the "strong protection [of data] against external access", noting that strong encapsulation comes at the cost of modularity and reusability. He observed that generic functions which can operate on multiple data types are more flexible, but weaken encapsulation and security and can result in unmanageable data structures. He suggested that encapsulation is just one of many trade-offs that must be made by software engineers between structure and flexibility. He noted data abstraction is concerned with encapsulation. He also noted that encapsulation is even more important in concurrent systems than in sequential programs, describing the abstraction boundary as the unit of encapsulation. In the same year, Mary Rosson and Sherman Alpert investigated the cognitive consequences of object-oriented design, and noted that encapsulation allows designers to chunk the object space, simplifying their mental representation of an evolving design.

In 1994, Fernando Brito e Abreu and Rogério Carapuça devised the Metrics for Object-Oriented Design (MOOD) designed to measure the quality of object-oriented software. Two of the metrics – the Method Hiding Factor (MHF) and Attribute Hiding Factor (AHD) – were designed to measure the degree to which methods and attributes within a class are encapsulated.

In 1995, Clark Archer and Michael Stinson, in the context of object-oriented software, defined encapsulation as the "process of bundling together the elements of an abstraction that constitute its structure and behavior" and information hiding as the "process of hiding the structure of an object and the implementation details of its methods".

In 1996, Abreu and Walcélio Melo investigated the ability of the MOOD metrics to predict defects within a system, and found that the MHF was moderately correlated with defects density and severity, but that the AHF was not significantly correlated. They noted that encapsulation mechanisms support information hiding by allowing components to be turned into black boxes thereby reducing side-effects.

In 1997, Rachel Harrison et al. described encapsulation as synonymous with information hiding, and listed it as one the three key elements of object-oriented software alongside abstraction and inheritance. They noted that friend classes in C++ allow encapsulation to be violated, recommending that they be used with care.

In 1998, Harrison et al. noted that data encapsulation or encapsulation is often understood to be "the power of a language to hide implementation details" by use of modules or opaque types. In contrast, they defined information hiding as "the extent to which methods and attributes are visible to code in other classes". They claimed that information can be hidden without being encapsulated and vice-versa. In the same year, Bjarne Stroustrup noted that object-oriented programming is often described as "encapsulation plus polymorphism plus inheritance", but noted that Simula 67 showed that object-oriented programming could be done without encapsulation.

In 2000, Scott Meyers discussed how the use of non-member functions in C++ increases encapsulation as it reduces the amount of code that can see the implementation details of a class. He thus recommended that class interfaces should be minimal, containing no methods which could be implemented as non-member functions. However, he also noted that encapsulation is just one of many competing concerns in interface design, and that minimal class interfaces are not always the best. He notes that encapsulation is desirable because it yields increased flexibility and robustness. He describes a data structure with only public data members as being unencapsulated. He suggests that there can exist different degrees of encapsulation, and that the degree to which a piece of code is encapsulated is determined by the amount of code that would be broken if it were changed. He notes that a public interface is used by an unknowably large amount of other code, and so is completely unencapsulated. He also notes that

In 2001, William Paul Rogers suggested that encapsulation simply describes the language feature that allows bundling of data with methods that operate on the data, and is distinct from information hiding, which is a design principle that aims to protect classes from the implementation details of other classes. He noted that encapsulation does not guarantee, but does facilitate, information hiding, and plays no role in controlling access to internal data.