"An object can access the private members of another object of same type"
The above code gives a complier error saying that i has private access at b.i
That's fine..but when for the following code there is no error.In previous case when iam not able to access the private member of class B how come i'm able to access the private member of A.Here also the reference to the object is only passed and how come the private members are accessed.I'm a bit confused.I know that code inside a class can only access the private members but can't understand accessing private members using "." operator.
I hope my doubt is clear
For the second case - objects can see the private variables and methods of an object of the same type. The private keyword is class-private, not object-private.
When i posted the question my intention was quite similar to the question which is present in link sent by kailash.
Kailash, Thanks a lot. The link you provided is very nice. I understood that private means "private to class" and not to objects. But, is there anyway we can know implementation details of private variables etc.
Originally posted by vivien siu:
can someone explain what's the difference of class-private and object-private?
Well as this question is itself very abstract but what I can tell is, Class-Private means the members of that class are private because of that you cann't access those members outside of that class.
And about Object-Private there is nothing like that Object also some public members and private members, actually this depends upon on Class. But Yes If any member of class is declared private then you cann't access those members outside of that class, even if you are creating Object of that class anywhere outside. because visibility of that member is in that class itself.
Hope it clears you.
Class-Private means the members of that class are private because of that you cann't access those members outside of that class.
If any member of class is declared private then you cann't access those members outside of that class, even if you are creating Object of that class anywhere outside. because visibility of that member is in that class itself.
Originally posted by vivien siu:
thanks Ankur, but from your explanation, class-private and object-private sounded almost the same
No vivien As I told you that their is nothing like any Object-Private word... That was the explanantions what you can understand in terms of technology.
Access Modifiers exists in terms of Class.
And if you create an Object of that class, that doesn't means that if you create a public Object then you can be able to access the private members. Because it's in the other class, and private members are no more visible outside that particular class.
So that doesn't matters whether you create a public Object or Private Object for accessing Class members.
Hope it clears You. Revert back if any concern.
But what i'm trying to understand is "how" the members variables of the object are being cabable of access.
According to my knowledge, all the constructors and methods of a class(except static methods) are given a reference to the object of that class, which we call as "this".
Using "this" a class can access the members as this.mem_name or since this is implicit we directly use the mem_name in the methods. But "this" only refers to the that particular Object. "How" come we are able to access the members as A.i in the code above, when that is not possible out side the definition.
I did clearly understand that private means class-private and that members of same object can be acessed.What i can't understand is "how" by augmenting with my knowledge about accessing members with "this".
I would be very happy with a different answer than to state the rules again and say "that's the way it works".