*** Welcome to piglix ***

Identity (object-oriented programming)


An identity in object-oriented programming, object-oriented design and object-oriented analysis describes the property of objects that distinguishes them from other objects. This is closely related to the philosophical concept of identity.

A reference 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. Typically, references are isomorphic to memory addresses. However, multiple such references can refer to the same object, if some form of address mapping is present (virtual addresses / page tables / memory segments).

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 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 programming.


...
Wikipedia

...