CSC 205/206/405
Professor Stearns
Design Issue - Inheritance Vs. Object Composition

Note: This is a fundamental design issue in object-oriented programming. Complete understanding of the tradeoffs is essential to an OO designer.
Bill Veneer devotes an entire article to the topic.

First some definitions:

Inheritance is a mechanism for implementing generalization, in which the behavior of a superclass is shared by all its subclasses. Sharing of behavior is justifiable only when a true generalization relationship occurs, that is, only when it can be said that this subclass IS A form of the superclass. Operations of the subclass that override the corresponding operation of the superclass have a responsibility to provide the same services. (Rumbaugh, pg. 244)

Object composition is assembling or composing objects to get more complex behavior.

When reusing code, should the designer use inheritance (to reuse the class code) or object composition (to add new functionality by composing two or more objects into a single object)?

Example: There is a Student class in our design; we need to add a GradStudent.

Inheritance:

class GradStudent extends Student
Composition:
class GradStudent {
    Student s;
    ......
Arguments for inheritance

Arguments for object composition


Last updated on 2/11/00