Chapter 13 Aggregation, Composition, and Inheritance Flashcards
When an object is complex, you shouldn’t use just one class. How should you break it up?
Into constiuent parts, defining one class as the whole and the other classes as part of the whole.
What is it called when the “whole” class is the exclusive owner of the parts classes?
A composition
In a composition relationship, how many owners can a composition part have?
Only one.
Example composition hierarchy, human body example.
What is the relationship between a containing class and one of its part classes know as?
A has-a relationship.
Example: Each human body has a brain and has a heart.
What type of has-a relationship is it when one class is whole and other classes are parts of the whole?
Aggregation, similar to composition, but there are no additional constraints that requires parts to be exclusively owned by the whole.
What does a Universal Modeling Language (UML) class diagram show?
The relationship between a programs classes.
What do the symbols in a UML mean?
Example also attached
- A solid line between two classes represents an association - a relationship between classes.
- On an association line, a solid diamond indicates a composition relationship, and a hollow diamond indicates an aggregation relationship. The diamond goes next to the container class.
- The labels on the association lines are called multiplicity values. They indicate the number of object instances for each of the two connected classes.
- The * multiplicity value represents any size number, zero through infinity.
What is required to implement a program that uses aggregation and composition?
- Define one class for the whole and define seperate classes for each of the parts.
- For a class that contains another class, declare an instance variable inside the containing class such that the instance variable holds a reference to one or more of the contained class’s objects.
- Typically, for association line with * multiplicity values, use an ArrayList to implement the instance variable associated with the asterisked class.
What are other considerations when implementing a program that uses aggregration and composition?
- If two classes have an aggregation relationship with non-exclusive ownership, then store the contained class’s object in an instance variable in the containing class, but also store it in another variable outside of the containing class, so the object can be added to another aggregation and have two different “owners.”
- If two classes have a composition relationship, then store the contained class’s object in an instance variable in the containing class, but do not store it elsewhere. That way, the object can have only one “owner.”
What is inheritance?
When a new class is derived from an existing class.
Why is it called inheritance?
Because the new class inherits/borrows all the features (data and methods) of the existing class.
Why is inheritance important to programming languages?
Because it allows programmers to reuse existing software. More specifically, the existing class is used by all of the classes that are derived from it.
Example inheritance hierarchy that keeps track of people in a department store
In an inheritance hierarchy, the pairs of classes are linked together. For each pair what are the classes called?
The more generic class is call the superclass and the more specific class is called the subclass.