Home » C++ » Access private field of another object in same class

Access private field of another object in same class

Posted by: admin November 30, 2017 Leave a comment

Questions:
class Person 
{
   private BankAccount account;

   Person(BankAccount account)
   {
      this.account = account;
   }

   public Person someMethod(Person person)
   {
     //Why accessing private field is possible?

     BankAccount a = person.account;
   }
}

Please forget about the design. I know that OOP specifies that private objects are private to the class. My question is, why was OOP designed such that private fields have class-level access and not object-level access?

Answers:

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 upvote Artemix’ answer. I’m just copy-pasting it.

Questions:
Answers:

See the Java Language Specification, Section 6.6.1. Determining Accessibility

It states

Otherwise, if the member or constructor is declared private, then
access is permitted if and only if it occurs within the body of the
top level class (§7.6) that encloses the declaration of the member or
constructor.

Click the link above for more details. So the answer is: Because James Gosling and the other authors of Java decided it to be that way.

Questions:
Answers:

Good question. It seems that object level access modifier would enforce the Encapsulation principle even further.

But actually it’s the other way around. Let’s take an example. Suppose you want to deep copy an object in a constructor, if you cannot access the private members of that object. Then the only possible way is to add some public accessors to all of the private members. This will make your objects naked to all other parts of the system.

So encapsulation doesn’t mean being closed to all of the rest of the world. It means being selective about whom you want to be open to.

Questions:
Answers:

Because the private access modifier renders it visible only within the class. This method is still IN the class.

Questions:
Answers:

the private field is accessible in the class/object in which the field is declared. It is private to other classes/objects outside of the one it is located in.

Questions:
Answers:

This works because you are in the class Person – a class is allowed to poke inside it’s own type of class. This really helps when you want to write a copy constructor, for example:

class A
{
   private:
      int x;
      int y;
   public:
      A(int a, int b) x(a), y(b) {}
      A(A a) { x = a.x; y = y.x; }
};

Or if we want to write operator+ and operator- for our big number class.

Questions:
Answers:

Are you trying to say that no object should be able to access this private attribute through any possible way? what is the point of having this attribute than? In a way this attribute is contributing to the state of the object but that state is not know to any body else.

Questions:
Answers:

With reflection concept in Java is possible modify fields and methods privates

Modificando metodos y campos privados con Refleccion en Java

Questions:
Answers:

First thing here we have to understand is all we have to do is must follow oops principles so encapsulation is say that wrap data within package(i.e. class) and than represent all data as Object and easy to access. so if we make field as non-private than
it’s accessed indivisually. and it result into bad paratice.

Questions:
Answers:

Just my 2 cents on the question of why the semantics of the private visibility in Java is class level rather than object level.

I would say that convenience seems to be the key here. In fact, a private visibility at object level would have forced to expose methods to other classes (e.g. in the same package) in the scenario illustrated by the OP.

In truth I was not able neither to concoct nor to find an example showing that the visibility at class-private level (like offered by Java) creates any issues if compared to visibility at object-private level.

That said, programming languages with a more fine-grained system of visibility policies can afford both object visibility at object level and class level.

For example Eiffel, offers selective export: you can export any class feature to any class of your choice, from {NONE} (object-private) to {ANY} (the equivalent of public, and also the default), to {PERSON} (class-private, see the OP’s example), to specific groups of classes {PERSON, BANK}.

It’s also interesting to remark that in Eiffel you don’t need to make an attribute private and write a getter to prevent other classes from assigning to it. Public attributes in Eiffel are by default accessible in read-only mode, so you don’t need a getter just to return their value.

Of course you still need a setter to set an attribute, but you can hide it by defining it as “assigner” for that attribute. This allows you, if you wish, to use the more convenient assignment operator instead of the setter invocation.

Questions:
Answers:

Private in java is class level access as you wrote.
Scala has also object private written as private[this] and other various ways detailed here http://alvinalexander.com/scala/how-to-control-scala-method-scope-object-private-package

Most probably private class level access in java was considered enough and is in sync with what c++ had at that moment.

I assume this was enough in java because a class was written in a single file so an author could decide to access members between different instances if it really wanted to. In scala you can inherit behavior from multiple traits and this might change the game.