Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access private field of another object in same class

Tags:

java

c++

.net

oop

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?

like image 814
Nageswaran Avatar asked Jun 10 '13 15:06

Nageswaran


People also ask

Can objects of same class access private variables?

Any method within your class can access the private data of any instance of that class; there's not a way to keep data private to within an instance unless you forbid methods that explicitly access private data members of other instances.

Can I access a private function of a class in the same class?

You can access private members from any code block that is defined in the same class. It doesn't matter what the instance is, or even if there is any instance (the code block is in a static context). But you cannot access them from code that is defined in a different class.

How do you access a private field that is a member of a class?

If we want to access Private Field and method using Reflection we just need to call setAccessible(true) on the field or method object which you want to access. Class. getDeclaredField(String fieldName) or Class. getDeclaredFields() can be used to get private fields.

Can an instance of a class access private members?

Well, it's all one class, so you can access your own private members. If you don't want to update secret for example, just don't. Coding can't protect you from yourself. The definition of private says only instances of Foo can access it.


2 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 vote Artemix' answer. I'm just copy-pasting it.

like image 143
Iwan Satria Avatar answered Oct 07 '22 11:10

Iwan Satria


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.

like image 23
Wei Qiu Avatar answered Oct 07 '22 11:10

Wei Qiu