Generalization



Generalization identifies commonalities among a set of entities. The commonality may be of attributes, behavior, or both. For example, a statement such as "All graphica user interface windows have a title" expresses a common attribute among all entities that are considered windows in a graphical user interface. Similarly, the statement, "All graphical user interface windows can be resized." expresses a common behavior that all windows provide. Generalizations are usually easy to recognize as they contain words like "all" and "every".

Generalization may be defined as:

Generalization
the identification, and possible organization, of common properties of abstractions.


This definition shows that generalization is not abstraction although the two are often confused. Abstraction aims at simplifying the description of an entity while generalization looks for common properties among these abstractions.

Generalizations are clearly important and prevalent in many disciplines of study. In science and mathematics, for example, the statements of "laws" and "theorems" are often generalizations - they state some property that holds over a group of things: the more powerful the generalization, the more things to which the generalization applies. The search for the basic forms of matter represents the physicists' quest for a generalization that applies to everything in the physical universe. The biologist's use of generalization is reflected in the organization of plants and animals into the taxonomy whose divisions are kingdom, phylum, class, order, etc.

Generalizations are equally important to software. Much of the effort in building software systems is to allow parts of the system to operate in the most general way possible. In some cases this might mean designing the system so that it can handle any number of things of the same kind. For example, the system might be expected to process any number of lines of input. In other cases the major design problem is how to handle things of different kinds or types. For example, the system might be expect to process input that comes from files of different formats or from both local as well as remote locations. An approach to solving some of these problems is to be able to capture more completely in software the idea of generalization.

One form of generalization is hierarchy. In the case of hierarchy, the commonalities are organized into a tree structured form. At the root of any subtree are found all the attributes and behavior common to all of the descendents of that root. This particular kind of tree structure is referred to as a generalization/specialization hierarchy because the root provides more general properties shared by all its descendents while the descendents typically add specializing properties which make them distinct among their siblings and their siblings' descendents.

Hierarchy

A hierarchical organization of components based on a relationship of generalization/specialization is an important device in object-oriented programming. While the power of such an organization can only be fully appreciated after more study, it is useful to at least hint at the role it will play. A generalization and its specializations are often said to be related by an "is-a" relationship. The relationship has this name because it is suggested by phrases such as "an aircraft is a vehicle" and "a lion is a carnivore". In these phrases the characteristics of a specialized entity, the aircraft and lion, are related to the characteristics of a more general category of entities, the vehicle and carnivore categories, respectively. The "is-a" terminology reflects that the specialization has all of the attributes and behavior of the generalization. Thus, an aircraft has all of the properties of vehicle plus additional one that discriminate it as an aircraft apart from other types of vehicles. Similarly, a lion has all of the properties of a carnivore in addition to those that make is uniquely a lion.

A more comprehensive intuitive example of a generalization/specialization hierarchy is one that organizes and relates the characteristics of different types of vehicles. The diagram in Figure 1.6.1 illustrates a possible hierarchical structure for vehicles. The arrows in this diagram represent the "is-a" relation. For example, the arrow connecting the categories Aircraft and Vehicle is read as "Aircraft is a kind of Vehicle." The general concept of Vehicle is the root of the hierarchy. Properties shared by all vehicles might include the manufacturer's name, the type of fuel required, and the maximum speed. Two distinct groups of vehicles are those intended for use on land, LandVehicles, and those designed to fly, Aircraft. Properties common to all land vehicles are the number of axles, and the vehicle identification number. While these properties make sense for LandVehicles, they are not relevant to Aircraft. Aircraft properties, however, might include a transponder signature, and a tail number. Both LandVehicles and Aircraft can be divided into Private and Commercial categories. Things in the Private category have a person-owner attribute while those in the Commerical category have a company-owner attribute. Various specific types of vehicles are shown for the LandVehicles categories and for the commerical subcategory of Aircraft.
 

 
A Generalization/Specialization Hierarchy for Vehicles

Another example of a generalization/specialization hierarchy, this time from the domain of graphical user interfaces, is shown in Figure 1.6.2. The most general element, Component, has attributes of location (coordinates denoting where the component appears on the screen) and shape (height and width), and a behavior that allows the component to be repositioned and resized. Specialized kinds of Components include an area for drawing graphical shapes (Canvas), an interactive element for user control (Button), and a general collection of components (Container). Two specialized kinds of Containers are a Window, a Container that has a visible representation on the screen, and a Panel, a Container that maintains an arrangement of other Components. The Frame is a Window with a border and a Dialog is a Window specialized for the purposes of creating a text-oriented interaction with the user. 

 


A Generalization/Specialization Hierarchy

The notion of hierarchy in object-oriented programming can be defined as follows:

Hierarchy
a generalization in which abstractions are organized into a directed acyclic graph whose arcs denote an "is-a" relation between a more generalized abstraction and the one or more derived specializations.
The most common case of hierarchy uses a tree structure to organize the abstractions, although more general organizations are possible.

A generalization/specialization hierarchy serves at least four major purposes. First, it provides a form of knowledge representation. A higher (more generalized) level in the hierarchy encodes an understanding of the general attributes and behavior possessed by all of its specialized descendents. Thus, it is possible to make statements such as: "All windows can be resized." and "All windows can be repositioned.". Second, the names of the intermediate levels in the hierarchy provide a vocabulary that can be used among developers and between developers and domain experts (those knowledgeable about the application domain but not necessarily about computing). This vocabulary allows discussions to be less ambiguous because the terms in the vocabulary identify specific, clearly defined concepts. Third, the hierarchy can be extended by adding new specializations at any level. These additions are easier to make because they occur within an existing framework that defines some, perhaps, many of their attributes and behavior. For example, to add a new specialized kind of Panel, it is not necessary to redefine all of the attributes and behavior already defined in Container, Component, and Panel; these are assumed to be part of the more generalized nature of the specialized Panel being added. Fourth, new attributes and behavior can be added easily to the proper subset of specializations. For example, any new attribute or behavior that might be needed for all containers can be added to the Container class. These additional attributes or behavior are then "automatically" part of all specialized kinds of Containers, but not of anything else.

Exercises.

  1. Define a hierarchy for "Printed Material" that would include such specializations as books, magazines, textbooks, newspapers, and others.
  2. Define a hierarchy for "Consumer Electronics" that would include such specializations as televisions, VCRs, radios, and others.
  3. Define a hierarchy for "Athletic Games" that would include such specializations as soccer, basketball, football, tennis, track, and others.