When you design a class, it is often useful to create one class that holds all the common properties and methods of the class and then create other subclasses that hold the unique properties and methods of that class. In OOP, the class that contains all the common properties and methods is called the base class. Let's look at an example that helps explain.
Assume that an investor calls you and wants to hire you to create a program to help her manage her real estate investments. The investor tells you that she owns three types of real estate: apartments, commercial office space, and private homes . You agree to take on the job. So, what's the first thing you should do?
Your first thought is probably to create a class for each of the different types of real estate. However, as you dig more deeply into the project, you realize that there are a number of things that all real estate investments have in common. Your preliminary design shows the following list of common properties:
There are probably others, but this serves as a starting point for defining the base class. You decide to name the base class Building .
"Is a" Relationships
If you have thought things through correctly thus far, the design should have a base class and one or more derived classes. A derived class is a class that inherits the properties and methods of the base class but is uniquely defined by additional properties and methods that are associated with the derived class. You might hear a derived class referred to as a subclass.
A properly designed derived class has an "is a" relationship to the base class. For example, you can say that an apartment "is a" building or that a home "is a" building. However, each of the derived classes Apartment and Home has additional properties and methods that uniquely define it. That is, properties of an apartment make it different from a private home. Likewise, a private home is different from a commercial building, even though both share some properties in common.
Now you ask yourself what properties uniquely identify the three types of real estate investments (apartments, commercial office space, and private homes). After a little thought, you come up with a table of properties, as shown in Table 17.1.
Table 17.1. Types of Buildings and Their Properties
As you can see from Table 17.1, several properties are similar across classes but not exactly the same (for example, rent). Other properties are unique to the specific building type (for example, bedrooms, occupancy rate). The table also shows the properties that all three building types share in common. These properties are the base class properties. You can think of the base class as the common denominator of the derived classes.
Why Have Base and Derived Classes?
Right now you might be thinking, "You're throwing all these new terms at me, and you even dredged up one term from my fifth-grade math class! What's the purpose?" The purpose is to make life easier for you in the long run.
To appreciate what inheritance gives you, let's pretend for a moment there is no inheritance. In that case, you would simply have an Apartment class, a Commercial class, and a Home class. Think about what the investor does to maintain her real estate investments. Each month, she collects the rents and pays the mortgages. Every three months, she pays the quarterly insurance premium for each building. Every six months, she pays the semi-annual property taxes for each building.
Given the activities of the investor, you need to write a CollectRent() method, a PayMortgage() method, a PayPropertyTaxes() method, and a PayInsurance() method for each class. Because there are three classes, you have to write 12 methods to cover these activities for all building types.
Then you have an epiphany. If every type of building has these activities in common, why not just write the methods once and let the different classes share these methods? Shazzam! Inheritance!
What are the benefits of using the inheritance approach? First, you have to write only 4 methods instead of 12. Sharing the methods reduces the amount of code you have to write. Second, when there is less code to write, there is also usually less time spent testing and debugging the code. With a smaller volume of code to contend with, it takes you less time to identify, isolate, and correct any bugs that may appear. Third, modifying and maintaining the code over time is also easier with inheritance. All these benefits make you a more productive programmer.
It makes sense to use inheritance when similar classes exhibit common behavior. Once you become used to base and derived classes, you'll wonder how you ever lived without them.