|
-
August 15th, 1999, 08:19 PM
#1
private access modifier
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
-
August 16th, 1999, 02:22 PM
#2
Re: private access modifier
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|