Inheritance Versus Aggregation

I l @ ve RuBoard

Inheritance is often misused ”the "inheritance is good, therefore, the more I use the better my code will be" syndrome. This is not true ”in fact, the misuse of inheritance can lead to problems. For example, a student may be full time or part time. A Student superclass with two subclasses ”FulltimeStudent and ParttimeStudent ”could be created. Multiple problems can arise with this structure. What happens if:

  • A full-time student decides to go to school only part time? This implies that an object would have to change its class.

  • Another dimension is added (e.g., on scholarship or not on scholarship)? Here you would have to add additional subclasses to model the scholarship information along with multiple inheritance to handle all the combinations (full-time student on scholarship, part-time student on scholarship, etc.).

Inheritance should be used to separate commonality from specifics. Aggregation should be used to show composite relationships. Often the two types of relationships are used together. The Student class has a classification (aggregation) that is either Fulltime or Parttime (inheritance). This is shown in Figure 8-5.

Figure 8-5. Inheritance versus Aggregation


I l @ ve RuBoard

Visual Modeling with Rational Rose 2002 and UML
Visual Modeling with Rational Rose 2002 and UML (3rd Edition)
ISBN: 0201729326
EAN: 2147483647
Year: 2002
Pages: 134

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: