Those three types of firefighter basically differ in

Info icon This preview shows pages 144–147. Sign up to view the full content.

View Full Document Right Arrow Icon
Those three types of firefighter basically differ in the strategy that they use for putting out fires. There’s a base class that provides a default implementation, and a couple of classes that override the virtual methods to do things differently. Let’s say we wanted to support lots of different types of firefighter, all of whom were expected to have a different approach to fighting fire, from the trainee, to the chief, to Gulliver (who has his own idiosyncratic way of putting out a fire in Lilliput). We still want the handy Name property and the Drive method, and we still want anyone to be able to call an ExtinguishFire method. 120 | Chapter 4: Extensibility and Polymorphism
Image of page 144

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

View Full Document Right Arrow Icon
Noticing that our FireChief , for example, doesn’t make use of the base implementation at all; we don’t want to provide a standard for that method. We’ll just let all imple- menters decide for themselves how it is going to work. We’re shooting for something that looks like Figure 4-2 . Figure 4-2. Abstract base classes Requiring Overrides with abstract An abstract base class is intended to provide the “scaffolding” for a hierarchy of related classes, but it is not intended to be instantiated itself, because it isn’t “finished.” It requires that classes derived from it add in some missing bits. Let’s turn our current firefighter into an abstract base for the others to use, and see how that works. First, we can add the abstract modifier to the class itself, and see what happens: abstract class Firefighter { // ... } As usual, we add the modifier before the class keyword (and after any accessibility modifiers, if present). If we build, we now get a compiler error: Cannot create an instance of the abstract class or interface 'Firefighter' Requiring Overrides with abstract | 121
Image of page 145
That’s because we’re trying to create an instance of the Firefighter in our main function: Firefighter joe = new Firefighter { Name = "Joe" }; This is no longer allowed, because the Firefighter class is now abstract. OK, we’ll comment that out temporarily while we carry on refactoring. We want it to continue to build as we go so that we can see if we’ve introduced any other errors: //Firefighter joe = new Firefighter { Name = "Joe" }; //joe.ExtinguishFire; Build and run, and we get the output we expect—Bill is still spraying the water around: Bill is putting out the fire! There's water going everywhere! Training the hose on the fire. One other thing: if we’re creating an abstract base class, we usually name it something such as FooBase to distinguish it from a regular class. This is by no means a hard-and- fast rule, but it is pretty common. So let’s rename Firefighter to FirefighterBase , and make sure we change it where it is referenced elsewhere—on the Firetruck , FireChief , and TraineeFirefighter classes.
Image of page 146

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

View Full Document Right Arrow Icon
Image of page 147
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