1

I have 2 classes, Base and child class. Base class has a protected int variable. My understanding is that any protected member of a class can only be accessed in a child class, It cannot be accessed from say creating an Instance of a class and accessing the protected member. How is this working then?

  class BaseClass
        {
            protected int x = 0;

            private static void Funny(BaseClass c, ChildClass d)
            {
                d.x = 9; // How can we access x here? 
                c.x = 0; // How can we access x here?
            }
        }

        class ChildClass: BaseClass
        {
            public void MyFunc()
            {
                x = 0; // This should be OK, because x is a protected member of 
            }

            private static void Funny(BaseClass c,ChildClass d)
            {

                d.x = 9;
                c.x=0;// This is giving compile time error? According to me even above line should give compile time error


            }
        }
2

In C#, the scope of access modifiers is the class, not the instance. This means that a private member can be accessed within the class on any instance.

public class Price
{
    private int amount;

    private static void CheckAmountOf(Price price)
    {
        // The following line is possible, even if `amount` is private and we are accessing an
        // instance of the object which was passed by a parameter.
        if (price.amount < 0)
        {
            throw new InvalidValueException("The price cannot be negative.");
        }
    }
}

This, by the way, makes it possible (and easy) to override Equals. Otherwise, you would be limited to public properties only, which may not be exactly what you want.

class Price
{
    private int amount;

    public override bool Equals(object obj)
    {
        ...
        var price = obj as Price;
        ...
        // The next line is valid, even if `amount` is private and even if the initial argument
        // was of type `object`. This way, you don't need to have a corresponding `Amount`
        // property with a public getter.
        return this.amount == price.amount;
    }
}
  • 1
    Wow, I didn't know that! – Charu Jun 4 '15 at 9:00
  • This answer doesn't actually explain why the final c.x=0 doesn't work, does it? – Ben Aaronson Jun 4 '15 at 12:37
1

MainMa's answer explains why most of the accesses work. However, to understand why the final one doesn't work, let's see what we could do if it was allowed:

class SiblingClass: BaseClass
{
}

    //...And then, inside some other code:
    var sibling = new SiblingClass();
    var child = new ChildClass();
    ChildClass.Funny(sibling, child);

(This also requires ChildClass.Funny to be made public)

This would all compile- sibling inherits from BaseClass so it could be passed in for a parameter of type BaseClass. But now a ChildClass can access a member on a SiblingClass. That's not what we expect from the protected keyword- we expect that to give access only to things it inherits from its parent, not to all other classes which also inherit from its parent.

So to prevent this unexpected access happening, it's made illegal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.