Thanks for you reply.
I thought the access control was designed in the purpose for
security......
And it is obviously a danger to let "a1" (of class A) to have access
to the private data of "a2".
If you wanna control the privileges of accesses from the outside
world, it's straightforward that any access from outside should be
controlled, whether it is from an instance of different class or the
same class.
Well, but you said class-based is not for security......
You have a serious misconception about access control and instances. A
class is a blueprint. Its a specification of what an instance will
look like and the interface an instance will have if and only if you
actually make one. You can't apply encapsulation to an instance
without first designing the model.
Our world works the same way. You buy a home that has a private
bathroom, your neighbour has the identical house. Does your neighbour
have access to your private bathroom because he has access to his? If
he flushes the toilet, does yours go too? Would making the bathroom
public change that? If you push the accelerator in your car, do all
the other cars on the road accelerate as well? Give all your
neighbours the key to your car and try pushing the accelerator again,
do the neighbour's cars accelerate too (are they even running)?
You and your neighbour share the same water reservoir/treatment plant.
If it breaks down, both you and your neighbour lose your water source
(the water plant is static, unique in your area - a translation unit).
It too is an instance of a water plant, although unique in your
sector. If you manage to bypass security and decide to bathe in that
water, does that mean that your body is simultaneously bathing in
every water plant this world has? Hmm?
Meanwhile: an instance is just an instance. An apple is just another
apple. You are just another person. The only thing these pairs of
entities have in common is their type.
If you do something silly and you end up in prison, thats not
encapsulation either, its Storage (you were placed in a secured
container). Its really a very simple concept. Just look around you.