Previous Section  < Free Open Study >  Next Section

2.5 Abstract Classes

In addition to the classes we have discussed so far, there is an important type of abstraction we need to explore. Consider the following questions: Have you ever eaten a fruit? How about an appetizer? What about a dessert? Many people answer yes to all three questions. In the event that you answered yes to any of these questions, consider these questions: What does a fruit taste like? How many calories are in a dessert? How much does an appetizer cost?

I claim that nobody has eaten a fruit. Lots of people have eaten apples, bananas, and oranges, but nobody has eaten a .3-pound red fruit. Likewise, a waiter approaches you in a restaurant and asks you what you would like for dinner. You answer, "an appetizer, an entree, and a dessert." If the waiter simply walks away, you've got a problem since you like shrimp cocktail but hate melon (two potential appetizers). We agree that there is no such thing as a fruit, appetizer, or dessert object, but these terms do capture useful information. If I held up an alarm clock and said, "What do you think of my fruit?" you would think I was crazy. If I held up an apple and asked the same question, you would consider me sane. The notion of fruit captures useful information even though you cannot create objects of it. It is, in effect, a class (concept) that does not know how to instantiate objects of its type.

Classes that do not know how to instantiate objects are called abstract classes.

Classes that do know how to instantiate objects are called concrete classes.

Watch out for the commonly used term abstract data type, or ADT. It is sometimes used as a synonym for class with no distinction between abstract class and concrete class.

An important use of abstract classes in the object-oriented paradigm is to facilitate the construction of inheritance hierarchies, that is, they connote the notion of category headings (see Figure 2.10). We will discuss their usefulness in Chapter 5.

Figure 2.10. Classes capturing category information.

graphics/02fig10.gif

    Previous Section  < Free Open Study >  Next Section