CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 1999
    Location
    Houston,Tx
    Posts
    9

    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



  2. #2
    Join Date
    Apr 1999
    Location
    Chennai, INDIA
    Posts
    27

    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
  •  





Click Here to Expand Forum to Full Width

Featured