RELATIONSHIP AMONG CLASSES

Consider for a moment the similarities and differences among the following classes of objects: flowers, daises, red roses, yellow roses, petals, and lady bugs.We can make the following observations:

  • A daisy is a kind of flower.
  • A rose is a different kind of flower.
  • Red roses and yellow roses are both kinds of roses.
  • A petal is a part of both kinds of flowers.
  • Ladybugs eat certain pests such as alphids,which may be infesting certain kinds of flowers

From this example we conclude that classes, like objects, do not exist in isolation .Rather a particular problem domain, the key abstractions are usually related in a variety of interesting ways, forming the class structure of our design.

We establish relationship between two classes for one or two reasons.First ,a class relationship might indicate some sort of sharing .For example ,daises and roses are both kind of flowers ,meaning that both have brightly colored petals , both emits fragrance, and so on.second a class relationship between might indicate some kinds of semantic connections.Thus,we say that red roses and yellow roses are more alike than are daises and roses ,and daises and roses are more closely related than are petals and flowers .Similarly ,there is a combining connection between ladybugs and flowers :ladybugs protect flowers from certain pests, which in turns serves as the food source for the lady bug. In all there are three basic kind of relationships. The first of these is generalization /specialization, denoting an “is a “relationship. For instance a rose is a kind of flower, meaning that a rose is a specialized subclass of the more general class, flower. The second is the whole /part, which denotes a part of relationship. Thus a petal is not a kind of flower; it is a part of flower. The third is a association ,which denotes some semantic dependency among otherwise unrelated classes ,such as between ladybugs and flowers .As another example roses and candles are largely independent classes , but they both represent things that we might use to decorate a dinner table.

Several common approaches have evolved in programming languages to capture generalization /specialization, whole /part, and association relationships. Specifically, most object-oriented languages provide direct support for some combination of the following relationship.

  • Association
  • Inheritance
  • Aggregation

« Previous PageNext Page »