Why Do Objects of the Same Class Have Access to Each Other'S Private Data

C++ - Why can a passed object's private variables be accessed directly in the Copy Constructor?

That's because access specifiers are effective per-class, not per-object. So a class method can access private members of any instance of the class.

All members of a class (bodies of member functions, initializers of member objects, and the entire nested class definitions) have access to all the names to which a class can access. A local class within a member function has access to all the names the member function itself can access.

private member variable is accessed by another object of same class?

you are not accessing c.value outside of the c object.

What you are doing is accessing a private variable of a class in the same class. Because the c variable is of exactly the same type as the class, you can access private variable c.value in that class.

Imagine the following situation

public class HelloWorld 
{
public static void main(String[] args)
{
OtherClass myObject = new OtherClass(7);
OtherClass yourObject = new OtherClass(4);
yourObject.value = 23;
System.out.print(myObject.compareTo(yourObject));
}
}

public class OtherClass
{
private int value;
OtherClass(int value)
{
this.value = value;
}

public int compareTo(OtherClass c)
{
return this.value - c.value;
}
}

Your code would not compile because value is not accessible from any class other than OtherClass. However, if you try to access c.value you will certainly succeed in doing that.

You would understand better if you study encapsulation more, and a good source of information is the [official documentation](https://docs.oracle.com/javase/tutorial/java/concepts/object.html"Oracle Documentation")

A private variable can be accessed from another object of the same type?

Methods of a class can access its private attributes throughout all class instances, at least in C++.

Access private field of another object in same class

I am also a bit curious with the answer.

The most satisfying answer that I find is from Artemix in another post here (I'm renaming the AClass with Person class):
Why have class-level access modifiers instead of object-level?

The private modifier enforces Encapsulation principle.

The idea is that 'outer world' should not make changes to Person internal processes because Person implementation may change over time (and you would have to change the whole outer world to fix the differences in implementation - which is nearly to impossible).

When instance of Person accesses internals of other Person instance - you can be sure that both instances always know the details of implementation of Person. If the logic of internal to Person processes is changed - all you have to do is change the code of Person.

EDIT:
Please vote Artemix' answer. I'm just copy-pasting it.

private member accessible from other instances of the same class

This is the same as in C++ and Java: access control works on per-class basis, not on per-object basis.

In C++, Java and C# access control is implemented as a static, compile-time feature. This way it doesn't give any run time overhead. Only per-class control can be implemented that way.

Access private elements of object of same class

That's redundant. Foo already has access to all Foo members. Two Foo objects can access each other's members.

class Foo {
public:
int touchOtherParts(const Foo &foo) {return foo.privateparts;}
private:
int privateparts;
};

Foo a,b;
b.touchOtherParts(a);

The above code will work just fine. B will access a's private data member.



Related Topics



Leave a reply



Submit