Click to See Complete Forum and Search --> : private access modifier


Deepa Natarajan
August 15th, 1999, 08:19 PM
Hi,

With the following code I am able to access the private variable of another class.How does this explain data hiding concept.

public class Y extends X
{
public static void main(String[] arg)
{ int val;
X x = new X();
val = x.m1(2);
System.out.println(val);

}

}

class X {
private int y ;
public int m1(int a){
y =a ;
System.out.println("ans=" + y);
return y;
}

}

Thanks,
Deepa

pkraman
August 16th, 1999, 02:22 PM
Hi Deepa,
Actually the Private Variable is not accessed. The copy of the Private Var is returned. In the example, If the Var "y" is stored in the Memory Address "5000", and when the Statement "return y;" is executed, the value of the var is copied into another memory location ( say 6000) and the reference to that memory location(6000) is returned to the Var "val". This doesn't mean that the Private variable can be accessed.

Hope this clarifies your doubt.

Kalyan