Professional C__ - Marc Gregoire [43]
A real-world example of this might be the relationship between a zoo and a monkey. You could say that a zoo has a monkey or a zoo contains a monkey. A simulation of a zoo in code would have a zoo object, which has a monkey component.
Often, thinking about user interface scenarios is helpful in understanding object relationships. This is so because even though not all UIs are implemented in OOP (though these days, most are), the visual elements on the screen translate well into objects. One UI analogy for a has-a relationship is a window that contains a button. The button and the window are clearly two separate objects but they are obviously related in some way. Since the button is inside the window, we say that the window has a button.
Figure 3-3 shows various real-world and user interface has-a relationships.
FIGURE 3-3
The Is-A Relationship (Inheritance)
The is-a relationship is such a fundamental concept of object-oriented programming that it has many names, including subclassing, extending, and inheriting. Classes model the fact that the real world contains objects with properties and behaviors. Inheritance models the fact that these objects tend to be organized in hierarchies. These hierarchies indicate is-a relationships.
Fundamentally, inheritance follows the pattern A is a B or A is really quite a bit like B — it can get tricky. To stick with the simple case, revisit the zoo, but assume that there are other animals besides monkeys. That statement alone has already constructed the relationship — a monkey is an animal. Similarly, a giraffe is an animal, a kangaroo is an animal, and a penguin is an animal. So what? Well, the magic of inheritance comes when you realize that monkeys, giraffes, kangaroos, and penguins have certain things in common. These commonalities are characteristics of animals in general.
What this means for the programmer is that you can define an Animal class that encapsulates all of the properties (size, location, diet, etc.) and behaviors (move, eat, sleep) that pertain to every animal. The specific animals, such as monkeys, become subclasses of Animal because a monkey contains all the characteristics of an animal. Remember, a monkey is an animal plus some additional characteristics that make it distinct. Figure 3-4 shows an inheritance diagram for animals. The arrows indicate the direction of the is-a relationship.
FIGURE 3-4
Just as monkeys and giraffes are different types of animals, a user interface often has different types of buttons. A checkbox, for example, is a button. Assuming that a button is simply a UI element that can be clicked and performs an action, a Checkbox extends the Button class by adding state — whether the box is checked or unchecked.
When relating classes in an is-a relationship, one goal is to factor common functionality into the superclass, the class that other classes extend. If you find that all of your subclasses have code that is similar or exactly the same, consider how you could move some or all of the code into the superclass. That way, any changes that need to be made only happen in one place and future subclasses get the shared functionality “for free.”
Inheritance Techniques
The preceding examples cover a few of the techniques used in inheritance without formalizing them. When subclassing, there are several ways that the programmer can distinguish an object from its parent object or superclass. A subclass may use one or more of these techniques and they are recognized by completing the sentence A is a B that . . . .
Adding Functionality
A subclass can augment its parent by adding additional functionality. For example, a monkey is an animal that can swing from trees. In addition to having all of the behaviors of Animal, the Monkey class also has a swing from trees behavior, which is specific to only the Monkey class.
Replacing Functionality
A subclass can replace or override a behavior of its parent entirely. For example, most animals move by walking, so you might give the Animal class a move behavior that simulates walking. If that’s the