Composition is the use of one reference data type in the creation of another. This is accomplished quite simply by declaring one or more field variables in a new class to be of a data type defined by another class. For example, a Ship data type might be composed by declaring field variables of the HullCharacteristics and HullGeometry types in the Ship class definition:
public class Ship { // class declaration
// Field Variables:
private HullCharacteristics charac; // dimensions and coefficients
private HullGeometry geom; // explicit shape
// Field Access Methods:
public void setGeometry ( // method to set the hull geometry
HullGeometry geo_value) { // argument supplying new geometry coordinates
geom = geo_value; // set the field to the supplied coordinates
} // end of setGeometry() method
// method to get the hull geometry
public HullGeometry getGeometry () {
return geom; // give the hull geometry to the caller
} // end of getGeometry() method
} // end of class
Note that a reference type can be used as the data type for method arguments
and return values as well as the data type in the declaration of class
fields. This is because a reference type is functionally equivalent in
every way to a pre-defined data type. Also note that methods to access the
'geom' field would normally also be defined, but are omitted here for
simplicity.Composing a new type out of other per-existing types (both pre-defined and reference) is very powerful, as it gives us the ability to easily model data abstractions of enormous conceptual size. Consider for a moment the following compositional hierarchy of reference data types:
Nucleus composed of integer fields for number of protons and neutrons
Electron composed of integer field for valence ring
Atom composed of Nucleus field and Electron fields
Molecule composed of Atom fields
Solid composed of Molecule fields
Liquid composed of Molecule fields
Gas composed of Molecule fields
Star composed of Gas fields
Planet composed of Solid, Liquid, and Gas fields
SolarSystem composed of Star, Planet, and Gas fields
Galaxy composed of SolarSystem and Gas fields
GalacticCluster composed of Galaxy and Gas fields
Universe composed of GalacticCluster fields
In principle, with only nine levels of composition we can model everything
in the Universe from the Universe itself down to the subatomic level. With
this underlying model of classes, our program can then simulate (very
crudely!) the moment of creation with the simple object creation statement
Universe u = new Universe();
This one single object creation statement could then create the Universe
object u and all of its class-defined constituent parts down to electrons
and atomic nucleii, and all objects of all intermediate types (but we better
have a really big computer!).While few of us will ever need to create a Universe modeling program, it does accurately depict the succinct power of expression inherent in the concept of composition. The real world makes good use of this concept. Now, with OO software technology, so too can our computer programs.