Protected Access
a private member of a base class is not accessible to a derived class. This would seem to imply that if you wanted a derived class to have access to some member in the base class, it would need to be public. Of course, making the member public also makes it available to all other code, which may not be desirable. Fortunately, this implication is untrue because C# allows you to create a protected member. A protected member is public within a class hierarchy, but private outside that hierarchy.
A protected member is created by using the protected access modifier. When a member of a class is declared as protected, that member is, with one important exception, private. The exception occurs when a protected member is inherited. In this case, a protected member of the base class becomes a protected member of the derived class and is, therefore, accessible to the derived class. Therefore, by using protected, you can create class members that are private to their class but that can still be inherited and accessed by a derived class. Here is a simple example that uses protected:
Example
using System; namespace ConsoleApplication1 { class B { protected int i, j; // private to B, but accessible by D public void Set(int a, int b) { i = a; j = b; } public void Show() { Console.WriteLine(i + " " + j); } } class D : B { int k; // private // D can access B's i and j public void Setk() { k = i * j; } public void Showk() { Console.WriteLine(k); } } class ProtectedDemo { static void Main() { D ob = new D(); ob.Set(2, 3); // OK, known to D ob.Show(); // OK, known to D ob.Setk(); // OK, part of D ob.Showk(); // OK, part of D } } }
In this example, because B is inherited by D and because i and j are declared as protected in B, the Setk( ) method can access them. If i and j had been declared as private by B, then D would not have access to them, and the program would not compile.
Like public and private, protected status stays with a member no matter how many layers of inheritance are involved. Therefore, when a derived class is used as a base class for another derived class, any protected member of the initial base class that is inherited by the first derived class is also inherited as protected by a second derived class.
Although protected access is quite useful, it doesn’t apply in all situations. For example, in the case of TwoDShape shown in the preceding section, we specifically want the Width and Height values to be publicly accessible. It’s just that we want to manage the values they are assigned. Therefore, declaring them protected is not an option. In this case, the use of properties supplies the proper solution by controlling, rather than preventing, access. Remember, use protected when you want to create a member that is accessible throughout a class hierarchy, but otherwise private. To manage access to a value, use a property.