Cover by Jennifer Greene, Andrew Stellman

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Chapter 6. Inheritance: Your object’s family tree

image with no caption

Sometimes you DO want to be just like your parents.

Ever run across an object that almost does exactly what you want your object to do? Found yourself wishing that if you could just change a few things, that object would be perfect? Well, that’s just one reason that inheritance is one of the most powerful concepts and techniques in the C# language. Before you’re through with this chapter, you’ll learn how to subclass an object to get its behavior, but keep the flexibility to make changes to that behavior. You’ll avoid duplicate code, model the real world more closely, and end up with code that’s easier to maintain.

Kathleen does birthday parties, too

Now that you got your program working, Kathleen is using it all the time. But she doesn’t just handle dinner parties—she does birthdays too, and they’re priced a little differently. She’ll need you to add birthdays to her program.

image with no caption
image with no caption

We need a BirthdayParty class

Modifying your program to calculate the cost of Kathleen’s birthday parties means adding a new class and changing the form to let you handle both kinds of parties.

Here’s what we’re going to do:

Note

You’ll do all this in a minute—but first ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required