I am not able to understand what is actually meant by the stamement "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.
The key phrase in the quote you showed is "of the same type." Any code in class A can access the private members of any instance of class A. But code in class A cannot access the private members of any instance of class B, because B is a different type (a different class.) Make sense?
Ernest,Thanks for the reply. It does make sense, but i wanted to know how it is acutally implemented rater than taking it as a rule and remembering. I don't know if iam asking too much of details. 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.
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.
Joined: Nov 10, 2005
thanks Ankur, but from your explanation, class-private and object-private sounded almost the same
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.
Joined: Nov 10, 2005
when you say class-private, it means private methods and variables of the class.
and object-private means: private Object obj = new Object(); right?
Well, offcourse it does not matter if we create a public object or private object of a class to access the members of the class. 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".