Access Modifiers and Inheritance

The private and the protected access modifiers are especially important in connection with inheritance. This section looks closer at these modifiers, along with the ability to declare a class member protected, internal, or internal protected.

The protected Access Modifier

As mentioned in the analysis of Listing 16.3, a protected class member can be accessed only from within its own class (like a private member) and from its subclasses. You should avoid declaring data members to be protected because it breaks with the encapsulation principles discussed in Chapter 3, “A Guided Tour Through C#: Part I.” The next section looks more closely at this issue.

Accessing private Base Class Members

The Car class presented ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.