This section contains 294 words (approx. 1 page at 300 words per page) |
Composition is a term relevant to computer science in the context of object-oriented languages such as Java. In object-oriented programming, the act of creating one object from a relationship between several other objects is known as composition.
A non-computing example is that of an airplane. An airplane has engines, wings, tires, wiring, and a myriad of other items that comprise it. To describe an airplane, one could not simply say wings. An airplane is an amalgamation of all the component parts, which are related to each other in the functional sense of being part of the whole. In computer science, composition relates the parts of a whole to both create a new object and to relate the components to the whole and to each other in a meaningful way.
Composition is also known as aggregation. It is different from inheritance, which is another means of establishing relationships between classes. In inheritance, one object is an extension of another object. For example, the class apple is an extension of the class fruit.
In composition, the components of the encompassing object are classified as separate classes. The encompassing object is itself a class. In the airplane example, two classes would be airplane and wings. These two classes are related, with wings being what is termed a field to the airplane class.
Composition is useful when coding specifications that relate objects have a "has" or "have" nature. The objects have a relationship that can be described as one object composing the other object. In contrast, inheritance is more of a "is a" relationship. When a programmer anticipates altering the code that would affect the relationship between the classes of concern, composition can be the technique of choice, as programming can be accomplished with greater ease.
This section contains 294 words (approx. 1 page at 300 words per page) |