Preview lessons, content and tests

Computer Science & Programming solved. All in one platform.

1. To trial the platform and take tests, please take a few seconds to SIGN UP and SET UP FREE.

2. Searching for something specific? See our text overview of all tests. Scroll right for levels, and lists.

3. Student and Teacher User Guides |  Schemes of Work |   Real Teacher use Videos |


Join 30000+ teachers and students using TTIO.

Aggregation

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 — “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:

  • Car has an Engine and a Wheel
  • Person has a Leg and an Arm
  • Book has 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!

class Car:
    def __init__(self, engine):
        self.engine = engine

class Engine:
    def __init__(self):
        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)

www.teachyourselfpython.com