I agree (or if I am under 13 my parent or guardian agrees on my behalf) to the terms and conditions of use and that: - My test statistics may be published on the site leaderboard against my username - My teacher(s) can review my test scores - I can receive feedback on my tests from my teacher(s)
In Object Oriented Programming, there are many different types of relationships which can exist between two or more classes. The most common two types are:
Inheritance — an “is a” relationship
Association — a “has a” relationship
The two types of Association relationships are Aggregation and Composition.
What is an Association relationship?
An association relationship between two classes is a “has a” relationship. For example:
A Carhas anEngine and a Wheel
A Personhas aLeg and an Arm
A Bookhas Pages
This usually represents when there are two classes, ClassA and ClassB, and either:
ClassA contains ClassB as an attribute, or
Instances of ClassB are constructed inside ClassA
Example of Aggregation
With an aggregation, the child can exist independently of the parent. Consider now a Car and an Engine ... the Engine doesn’t need to be destroyed when the Car is destroyed. This is the key idea. The engine component can be retained!
def __init__(self, engine):
self.engine = engine
passengine = Engine()
car = Car(engine) # If I destroy this Car instance,
# the Engine instance still exists
UML (Unified modelling language)
UML distinguishes between composition and aggregation. (These are often synonymous terms.)
Suggested Video (Aggregation and Composition in Python)