Identity (object-oriented programming)
Encyclopedia
An identity in object-oriented programming
, object-oriented design and object-oriented analysis describes the property of object
s that distinguishes them from other objects. This is closely related to the philosophical concept of identity
.
can be used to refer to an object with a specific identity. A reference contains the information that is necessary for the identity property to be realized in the programming language
, and allows access to the object with the identity. A type of a target of a reference is a role.
Object identity is less useful as a semantic concept in environments or situations in which the structure of objects is not encapsulated, and two objects are considered to be the same object based on having identical properties, even if they are not actually the same physical instance (structural equivalence). However, object identity can nevertheless provide optimization. A function which tests whether two arguments are the same object can quickly short circuit to an affirmative answer if the two arguments have the same identity (are references to the same instance). Only if the argument are distinct objects do the individual properties need to be considered to determine equality, which is a more expensive operation. For instance, bignum integers may be heap-allocated objects such that two bignums are considered to be the same if they represent the same number. It would be a waste of machine cycles in the equality function not to take advantage of the discovery that the two arguments being compared are references to the same bignum.
.
Identity allows comparison of references. Two references can be compared whether they are equal or not. Due to the identity property, this comparison has special properties. If the comparison of references indicates that the references are equal, then it's clear that the two objects pointed by the references are the same object. If the references do not compare equal, then it's not necessarily guaranteed that the identity of the objects behind those references is different. The object identity of two objects of the same type is the same, if every change to either object is also a change to the other object.
or conceptual model, that is often used as basis of object-oriented thinking. The conceptual model describes the client
side view to a domain
, terminology or an API
. This world contains point-like objects as instances, properties of the objects and links between those objects. The objects in the world can be grouped to form class
es. The properties of the objects can be grouped to form roles. The links can be grouped to form association
s. All locations in the world together with the links between the locations form the structure of the world. These groups are types of the corresponding instances of the world.
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
, object-oriented design and object-oriented analysis describes the property of object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...
s that distinguishes them from other objects. This is closely related to the philosophical concept of identity
Identity (philosophy)
In philosophy, identity, from , is the relation each thing bears just to itself. According to Leibniz's law two things sharing every attribute are not only similar, but are the same thing. The concept of sameness has given rise to the general concept of identity, as in personal identity and...
.
Identity and references
A referenceReference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...
can be used to refer to an object with a specific identity. A reference contains the information that is necessary for the identity property to be realized in the programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
, and allows access to the object with the identity. A type of a target of a reference is a role.
Object identity is less useful as a semantic concept in environments or situations in which the structure of objects is not encapsulated, and two objects are considered to be the same object based on having identical properties, even if they are not actually the same physical instance (structural equivalence). However, object identity can nevertheless provide optimization. A function which tests whether two arguments are the same object can quickly short circuit to an affirmative answer if the two arguments have the same identity (are references to the same instance). Only if the argument are distinct objects do the individual properties need to be considered to determine equality, which is a more expensive operation. For instance, bignum integers may be heap-allocated objects such that two bignums are considered to be the same if they represent the same number. It would be a waste of machine cycles in the equality function not to take advantage of the discovery that the two arguments being compared are references to the same bignum.
Consequences of identity
Identity of objects allows objects to be treated as black boxes. The object need not expose its internal structure. It can still be referred to, and its other properties can be accessed via its external behaviour associated with the identity. The identity provides a mechanism for referring to such parts of the object that are not exposed in the interface. Thus, identity is the basis for polymorphism in object-oriented programmingPolymorphism in object-oriented programming
Subtype polymorphism, almost universally called just polymorphism in the context of object-oriented programming, is the ability to create a variable, a function, or an object that has more than one form. The word derives from the Greek "πολυμορφισμός" meaning "having multiple forms"...
.
Identity allows comparison of references. Two references can be compared whether they are equal or not. Due to the identity property, this comparison has special properties. If the comparison of references indicates that the references are equal, then it's clear that the two objects pointed by the references are the same object. If the references do not compare equal, then it's not necessarily guaranteed that the identity of the objects behind those references is different. The object identity of two objects of the same type is the same, if every change to either object is also a change to the other object.
Identity and object-oriented conceptual model
Identity allows the construction of a platonic ideal world, the ontologyOntology (computer science)
In computer science and information science, an ontology formally represents knowledge as a set of concepts within a domain, and the relationships between those concepts. It can be used to reason about the entities within that domain and may be used to describe the domain.In theory, an ontology is...
or conceptual model, that is often used as basis of object-oriented thinking. The conceptual model describes the client
Client (computing)
A client is an application or system that accesses a service made available by a server. The server is often on another computer system, in which case the client accesses the service by way of a network....
side view to a domain
Application domain
In Microsoft's Common Language Runtime , an application domain is a mechanism used to isolate executed software applications from one another so that they do not affect each other...
, terminology or an API
Application programming interface
An application programming interface is a source code based specification intended to be used as an interface by software components to communicate with each other...
. This world contains point-like objects as instances, properties of the objects and links between those objects. The objects in the world can be grouped to form class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...
es. The properties of the objects can be grouped to form roles. The links can be grouped to form association
Association (object-oriented programming)
In object-oriented programming, association defines a relationship between classes of objects that allows one object instance to cause another to perform an action on its behalf...
s. All locations in the world together with the links between the locations form the structure of the world. These groups are types of the corresponding instances of the world.
See also
- Meyer: Object-oriented software construction, second edition
- http://www.cs.cmu.edu/People/clamen/OODBMS/Manifesto/htManifesto/node4.html