Constructing Simple Hierarchies with Composition
There is another common form of inheritance, very different from parent-child inheritance, called composition. Using an object in composition is easy. All you have to do is assign an object as the value for a private member variable. For example, if double represents a class, we could say that this.mSize is one element of the composition. This means that even simple classes use a composition of built-in types. Complex classes add structures and objects to the composition. Unlike parent-child inheritance where the parent's interface is public, the interface of every object in the composition remains private. In MATLAB, there is overlap between parent-child inheritance and composition. Parent-child inheritance is a special case of composition. The child is a primary object and the parent is a secondary object. This might seem backward but it is consistent with the way primary and secondary were defined. The parent is a secondary object because the parent object is stored as an element in the child's private structure.
Constructing Simple Hierarchies with Composition, Page 1 of 2
< Previous page Next page > /docserver/preview/fulltext/books/pc/sbpc001e/SBPC001E_ch15-1.gif /docserver/preview/fulltext/books/pc/sbpc001e/SBPC001E_ch15-2.gif