|
Generalization simply means identification of the commonalities among classes. The commonality may be in state, behavior, or both. For example, a problem description such as "All automobiles have a engine" expresses a common property (state) “engine” among all automobiles. Similarly, the statement, "All automobiles can be driven." expresses a common behavior among all automobiles.
![]() |
Figure 11.2 Animal hierarchy with generalized Animal class at root and specialized classes at bottom
The root class represents common (generalized) state and behavior shared among all its descendents. The subclasses, on the other hand, represent special behavior. For instance, the class Animal represents state and behavior common to all aircrafts. But its subclasses represent special behavior. For instance, Dog is a special kind of Animal and so is the Cat. But each of them is-an Animal. Thus, whenever a class represents “common to all” behavior, it represents generalization. Similarly, if the class IS-A special kind of some class, it represents specialization. Therefore, it is often easy to spot generalization when you look for any of the following phrases in problem definition.
"is a"
"is a type of"
"is a kind of"
Note that when you read the class hierarchy from bottom up, the IS-A relationship is revealed. For instance, when you read the animal hierarchy in figure 11.2, you can conclude that Dog IS-AN Animal. ShowDog IS-A Dog.
|
|
Generalization and Inheritance are closely related. Generalization represents the commonality among classes whereas Inheritance is the property of the generalization due to which the commonality of a class is available to the derived class. |