2
$\begingroup$

In the object oriented programming concepts I found the term "Identity of object". What does it mean by "Identity of object"?

$\endgroup$
  • 3
    $\begingroup$ Can you give more context? $\endgroup$ – adrianN Jul 13 '16 at 12:53
  • $\begingroup$ Where you found, can you explain your question properly?? $\endgroup$ – Maharaj Jul 13 '16 at 12:53
  • $\begingroup$ I found it in a question paper in the university. That paper says Identity of an object means that each object is unique even if its state is identical to that of another object. Can you further explain this term.? $\endgroup$ – dulaj sanjaya Jul 13 '16 at 12:58
  • $\begingroup$ Do you ask about Identity in OOP as in Wikipedia or just pointer / reference per object, where equality indicates the very same object without even looking at fields? $\endgroup$ – Evil Jul 13 '16 at 22:20
  • $\begingroup$ I think this is not related with the reference or pointer.It is related with the identity in OOP as in wikipedia Article.(I think this is related wth the concept of "if x and y share all their properties, are they one and the same thing?"). $\endgroup$ – dulaj sanjaya Jul 14 '16 at 2:02
4
$\begingroup$

Depending on the programming language, some or all objects might have reference semantics. What this means is that when you assign the object to a variable or pass it to a method, it's still the same object, not a copy. This means the object maintains identity independent of the values it holds.

For example, in C#, classes have reference semantics, while structs do not. This means that for a class C and struct S:

C c1 = new C();
c1.Name = "c1";
C c2 = c1;
c2.Name = "c2";
WriteLine(c1.Name); // writes "c2"
WriteLine(c1.Equals(c2)); // writes "True"

S s1 = new S();
s1.Name = "s1";
S s2 = s1;
s2.Name = "s2";
WriteLine(s1.Name); // writes "s1"
WriteLine(s1.Equals(s2)); // writes "False"

As you can see, changing the class instance through one variable affects the other variable too, showing that both refer to the same object. Also, you can see that the two variables are equal. So, classes in C# maintain object identity.

For the struct, each variable is independent: changing one does not affect the other. This means that structs in C# do not have object identity in C#.

Other languages behave differently. For example, Java does not have structs at all, all user-defined types have reference semantics.

As another example, in C++, you can decide to use reference semantics or not on a case-by-case basis, by choosing to use pointers (or references) or not.


Note: Equals() in C# can be overridden. But since C or S do not override it, the default implementation of Equals() mostly behaves the way we want for both.

$\endgroup$
  • $\begingroup$ You say "This means the object maintains identity independent of the values it holds.". But according to my term It says If two objects have same states,each is unique.(not both have same uniqueness, It says each is unique independently). $\endgroup$ – dulaj sanjaya Jul 14 '16 at 3:03
  • $\begingroup$ @dulajsanjaya - that's right: two reference objects with the same state are not the same object, i.e. they are both unique. In Java, for example, ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); return a == b; results in false, despite the two lists having identical state at the point of the comparison. $\endgroup$ – Jules Jul 14 '16 at 9:45
  • 1
    $\begingroup$ Actually I can't satisfy with this answer. Because I think this answer is not related to the "identity of object". It is related with the concept of "copy by value and copy by reference". $\endgroup$ – dulaj sanjaya Jul 14 '16 at 10:16
1
$\begingroup$

I think what is trying to be said here, is even if you have 2 objects that have the same state (i.e, two different instances of the same object that have the same values to their variables.

For example, let's assume we have a Java Class called Dog.

public class Person{
    private String breed;
    int weight;
    int height;
}

If I had created:

Dog dog1=new Dog("Lab", 15, 40);
Dog dog2=new Dog("Golden", 20, 20);
Dog dog3=new Dog("Lab", 15, 40);

You wouldn't say that dog1 and dog3 are the same dog, would you? Because you could identify that they are representing different dogs. However, in the sense of object representation (By its values), they are basically representing the same object.

If I had added a variable called "Name" to each dog, and giving each one a different name, we could no longer say that dog1 and dog2 have the same identity.

Hope it's all clear

$\endgroup$
  • $\begingroup$ Even if you add the name and it will be equal these are two different objects - that is identity, the equality has nothing to do with identification. $\endgroup$ – Evil Jul 13 '16 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.