Before we can do anything we need to make them

Info icon This preview shows pages 138–141. Sign up to view the full content.

View Full Document Right Arrow Icon
Before we can do anything, we need to make them accessible to derived classes. Inheritance and Protection In the preceding chapter, we mentioned that there were two additional accessibility modifiers that we would deal with later: protected and protected internal . Well, this is where they come into their own. They make members accessible to derived classes. If you want a member to be available either to derived classes or to other classes in your own assembly, you mark that member protected internal . It will be visible to other classes in the library, or to clients that derive classes from your base, but inaccessible to other clients who just reference your assembly. If, on the other hand, you want your class to make certain methods available only to derived classes, you just mark those methods protected . In terms of code out there in the wild, this is the most common usage, but it is not necessarily the best one! 114 | Chapter 4: Extensibility and Polymorphism
Image of page 138

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Both protected internal and internal are much underused access modifiers. They are a very convenient way of hiding away library im- plementation details from your consumers, and reducing the amount of documentation and surface-area testing you need. I suspect that they are unpopular (as with most “hidden by default” or “secure by default” schemes) because they can sometimes get in your way. There are a fair number of implementation details of classes in the .NET Framework that are internal (or private ) that people would very much like to access, for example. A common reason for taking something useful and applying the inter nal modifier is that it was not possible to fully document (or understand the full implications of) the “hook” this would provide into the frame- work. And rather than open up potential security or reliability problems, they are marked internal until a later date: perhaps much, much later, tending toward never. Although there is an intention to revisit these things, real-world pressures mean that they often remain unchanged. This is another example of the “lock down by default” strategy which helps improve software quality. That doesn’t make it any less irritating when you can’t get at the inner workings, though! So we’ll mark those methods in the base class virtual and protected , as shown in Example 4-10 . Example 4-10. Opening methods up to derived classes protected virtual void TurnOnHose() { Console.WriteLine("The fire is going out." ); } protected virtual void TrainHoseOnFire() { Console.WriteLine("Training the hose on the fire."); } We can now create our TraineeFirefighter class (see Example 4-11 ). Example 4-11. Overriding the newly accessible methods class TraineeFirefighter : Firefighter { private bool hoseTrainedOnFire; protected override void TurnOnHose() { if (hoseTrainedOnFire) { Console.WriteLine("The fire is going out." ); } Inheritance and Protection | 115
Image of page 139
else { Console.WriteLine("There's water going everywhere!" ); } } protected override void TrainHoseOnFire() { hoseTrainedOnFire = true; Console.WriteLine("Training the hose on the fire." ); } }
Image of page 140

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 141
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern