Can one object acess a private variable of another object of the same class ?
Ans: Yes
Could any one explaine this rule with one example?
Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Anupam, See variables are never overridden, they are hidden. This means that if the variables are called under class A's scope, variable of class A will be visible. While if its under class B's scope, variable of class B will be visible. So here since "name" variable is initialized to "World" in class B but this is hidden in class A, in A the same variable is null. Class A cannot see class B's varaible. So it displays null.
Raghu Shree
Ranch Hand
Joined: Mar 18, 2005
Posts: 143
posted
0
Hi Animesh 1. Class B has getName() method. Bcoz it is inherited from A. 2. Now class B object called the getName() method: b.getName(). 3. B has own memeber variable b and initialized as world 4. Why it is print null.
So here since "name" variable is initialized to "World" in class B but this is hidden in class A, in A the same variable is null.
But the below code is work fine.
Output: A value From A Scope 100 B value From B Scope 200 A value From B scope 100
How is it possible. I am Confused. Is anything I understand wrong?
Animesh Shrivastava
Ranch Hand
Joined: Jul 19, 2004
Posts: 298
posted
0
So whats the problem u r having, i guess its in this statement System.out.println("A Value From B Scope " +b.getA());
right? Ok whats happening here is that since methods are inherited from super class to sub class, so u can very well access the method getA(). Now since this method accesses the variable a, which is initialized to 100, u get the output. Now one more thing, see when u create an instance of a class, beore the instance is created constructor of super class is also run and in effect all its instance initializers. So lets see whats happening here
When u create an object of A, u also initialize the variable a to 100; When u create an object of B, u initailize A's instance members( A is super class of B, here a is initialized to 100) as well as instance members of class B U getting it? So when getA() is called from Class A, u get the variable 100 printed for a.
Now lets take up the previous example for u. In that example :
See above, i have modified the code for u. When u create an instance of Class A, u get name initialized for the class. When u create an instance of Class B, the no-arg constructor of class A is also run because by default JVM inserts the statement "super()". In ur earlier case, since A's default constructor was being run, u got the value as null for the string name. But now look at the code i have given, here in the no-arg constructor of A, i am also initializing the variable name of A. Run the program and tell me if u have any problem in justifying the output.
Thanks
Raghu Shree
Ranch Hand
Joined: Mar 18, 2005
Posts: 143
posted
0
Hi Animesh Thanks lot. Now I am cleared in Inheritance with member hiding.
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
posted
0
Hi Animesh
Thanks for the reply. I wanted to know that even when a (Object ref. to A) and b (Object ref to B) belong to two different classes then also
the above line executes.
In the code above an Object of type B is changing an Object of Type A.
So does it mean "Can one object acess a private variable of another object of the same class and a diferent class aswell.
Animesh Shrivastava
Ranch Hand
Joined: Jul 19, 2004
Posts: 298
posted
0
So does it mean "Can one object acess a private variable of another object of the same class and a diferent class aswell.
No an object cannot directly access the private variable of another object of the same class. It can only be done using a public method which accesses the variable , like its done using anotherName() method
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
[Animesh]: No an object cannot directly access the private variable of another object of the same class.
I guess that means you haven't actually tried it, eh?
"I'm not back." - Bill Harding, Twister
Raghu Shree
Ranch Hand
Joined: Mar 18, 2005
Posts: 143
posted
0
Hi jim, Is it possible? how ?
Joyce Lee
Ranch Hand
Joined: Jul 11, 2003
Posts: 1392
posted
0
Animesh: No an object cannot directly access the private variable of another object of the same class.
The example given by Mark has demonstrated that it's possible.