can we access a private variable using an object

前端 未结 7 1779
一整个雨季
一整个雨季 2020-12-11 08:07

We cannot access a private variable of a class from an object, which is created outside the class, but it is possible to access when the same object is created inside the cl

相关标签:
7条回答
  • 2020-12-11 08:42

    You can access i from within the class because private members can only be accessed by members of the class. In this case it looks strange because p is a different object than the object that accesses the variable, but its still the same class and the restriction is on the class level not on the object level.

    class Program
    {
        private int i;
    
        public void method1()
        {            
            Program p = new Program();
            p.i = 5;        // OK when accessed within the class
        }
    
    }
    

    You can not access i from within another class (unless it is an inner class but that's a different story). Which is completely as expected.

    class AnotherClass
    {
        void method2()
        {
            Program p = new Program();
            p.i = 5; //error because private variables cannot be accessed with an object which is created out side the class
        }
    }
    

    I understand the point you want to make. The restriction on class level looks counter intuitively. And maybe this is wrong. But the member variables are still only accessible from within the class, so you still have total control to guarantee the encapsulation of your privates.

    0 讨论(0)
  • 2020-12-11 08:46

    why??

    It's true by the language specification. The access modifier private has the semantics that only the class or struct declaring a member is allowed to access that member.

    I suggest reading the specification for details. In particular, check out

    §3.5.1 Declared Accessibility

    §3.5.4 Accessibility constraints

    §10.2.3 Access Modifiers

    §10.2.6.2 Declared Accessibility

    §10.2.6.5 Access to private and protected members of the containing type

    0 讨论(0)
  • 2020-12-11 08:47

    In both the cases above, we are accessing the private variable 'i' through the object 'p'. But inside class it is allowed, outside the class not allowed. Can anybody tell me the reason behind this??

    Because access modifiers don't pertain to the object, they pertain to the class (or assembly, for the internal modifier).

    Even if you access it from a different object, or a static context, as long as you stay in the same class, accessing a private member will work. It's private to the class, not its instances.

    0 讨论(0)
  • 2020-12-11 08:50

    You are maybe confusing object and class, public/private/protected/internal affect class visibility not object visibility. That means an object of a class can access attributes of another object of the same class, even if they are private.

    I am not sure I understood your question correctly...

    0 讨论(0)
  • 2020-12-11 08:50

    You Can Access the private variables in the following process also...

    namespace OOPSProj
    {    
        class Program
        {    
            private int i;    
            public void Methode1()
            {
                Program objprog2 = new Program();
                objprog2.i = 15;
                Console.WriteLine("i= " + objprog2.i);
            }    
            static void Main(string[] args)
            {
                Program objprog = new Program();
                objprog.i = 10;
                Console.WriteLine("i= " + objprog.i);
                objprog.Methode1();                
            }
        }    
        class Test
        {
            static void Main()
            {
                Program objproj3 = new Program();
                objproj3.Methode1();    
            }          
        }    
    }
    
    0 讨论(0)
  • 2020-12-11 08:59

    This belongs to OOP. The answer would be because its private, otherwise if all private variables would be accessable from outside the class the object's concept wouldnt make any sense.

    0 讨论(0)
提交回复
热议问题