#306 – Protected Class Members

Class members marked with the accessibility keyword protected are accessible from within the same class, or from within the code of any classes that derive from the class.

In the picture below, the Dog.DoBark method is marked as protected.  The code in any of the blue blocks can call this method.

Code in a subclass of Dog can call the DoBark method.

    public class Terrier : Dog
    {
        public void TerrierBark()
        {
            // CAN call DoBark from subclass
            this.DoBark();
        }
    }

Code in other classes, not derived from Dog, cannot call the DoBark method.

    public class OtherClass
    {
        public void DoSomething()
        {
            Dog d = new Dog();
            // This class CANNOT call DoBark()
            d.DoBark();
        }
    }
Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: