Home Tutorials Python OOP Concepts Aggregation
Aggregation

Aggregation


31. Aggregation

Aggregation is a form of association where one class uses another class object, but both can exist independently. It is also a "has-a" relationship, but with weaker ownership than composition.

Syntax

class B:
    def __init__(self, obj):
        self.obj = obj

Example

class Department:
    def __init__(self, name):
        self.name = name

class Teacher:
    def __init__(self, name, department):
        self.name = name
        self.department = department

d = Department("Computer Science")
t = Teacher("Ravi", d)
print(t.name)
print(t.department.name)

Output

Ravi
Computer Science
Example

🏋️ Test Yourself With Exercises

Take our quiz on Aggregation to test your knowledge.

Browse Quizzes »