开发者

Why Protected Members are only accessible by methods of Derived Class [duplicate]

开发者 https://www.devze.com 2023-04-10 05:07 出处:网络
This question already has answers here: Why can't I access protected variable in subclass? (2 answers)
This question already has answers here: Why can't I access protected variable in subclass? (2 answers) Closed 9 years ago.

I have the Parent Class :

class clsTestParent
    {
        protected int x;

        public void Foo()
        {
            x = 10;
        }

    }

I have the Derrived Class as below:

class clsDerivedTest : clsTestParent
    {

            x = 10;
            Foo();

    }

But this is not working as I am getting following two errors:

Invalid token '=' in class, struct, or interface member declaration

Method must have a return type

But the above statements works fine when I try to use them with a method in derived class as below:

 class clsDerivedTest : clsTestParent
    {


        public void myTestMethod()
        {
   开发者_运维问答         x = 10;
            Foo();
        }
}

Why the protected var or methods are only accessible by using derived class methods but not direcly in the class.

I even tried accessing the Protected member by creating object as below:

clsDerivedTest objDerivedTest = new clsDerivedTest();
            objDerivedTest.x = 10;

But again getting error for Protection Level. I have the var as protected so why the object of derived class can not access it?

I need to clear my fundamentals of OOPs but stuck here.


Well, method bodies is the only place where code can be executed, apart from initialisers. Your statement x = 10; is an assignment expression that should go in a method body.

So, this would work at the class level:

int x = 10;

because it is a field declaration plus initialiser. But a simple assignment statement does not work. Think of it this way: if you placed x = 10; in the middle of your class body, outside every method, when would it run? There would be no way to "invoke" it.

Create a method for it or put your assignment in the class constructor if you want it to run on object construction.


You know your problem - I need to clear my fundamentals of OOPs but stuck here.

Clear the concept of protected member:

The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

class A 
{
   protected int x = 123;
}

class B : A 
{
   void F() 
   {
      A a = new A();  
      B b = new B();  
      a.x = 10;   // Error
      b.x = 10;   // OK
   }
}

Also, it will be good if you go through C# language specification. It will make your understanding clear.


"Why the protected var or methods are only accessible by using derived class methods but not direcly in the class." What you are trying to do is to add code in the class body directly. This is not working. And there is now OO-language that allows this.

To initialize x to 10 you might use the constructor of the class...

class clsDerivedTest : clsTestParent     
{           
    public clsDerivedTest()         
    {             
        x = 10;             
    } 
}


You can't call a function in class body in c#.


Everything goes into method body, except for some declarations of variables. Calling a method goes into method body. When you have more than one class, the communication between them happens by using methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号