John Smith
John Smith

Reputation: 15

protected access modifier meaning of derived

class Program
{
    class TestA
    {
        protected string protectedStringA;
    }

    class TestB : TestA
    {
        string StringTestB()
        {
            return protectedStringA;
        }
    }

    class TestC : TestB
    {
        string StringTestC()
        {
            return protectedStringA;
        }
    }


    static void Main(string[] args)
    {


    }
}

As per this link the definition of protected is "The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class". Here the Class TestC is not derived from TestA. But the protectedStringA is still accessible. What is the exact meaning of class that is derived from that class?

Upvotes: 1

Views: 107

Answers (3)

Paulo Morgado
Paulo Morgado

Reputation: 14856

Let's put it on other words:

"Any class can access any protected members of its ancestors".

If TypeA is an ancestor of TypeB and TypeB is an ancestor of TypeC, than TypeA is an ancestor of TypeC.

Upvotes: 2

user3557236
user3557236

Reputation: 299

Even though TestC is not directly derived from TestA, it is indirectly because TestB inherits TestA. I hope this helps.

Upvotes: 2

Selman Genç
Selman Genç

Reputation: 101731

Since B inherits A's (public and protected) members, C inherits all of them from B. That's the point of inheritance.

What is the exact meaning of class that is derived from that class?

It means a derived class can access protected members of it's base class. In this case C is actualy inheriting from A indirectly.So it has access to all public and protected members of it's base classes.

Upvotes: 2

Related Questions