Aggregation
Introduction to OOP's
Class
Object
Constructor
Instance Variables
Class Variables
Instance Methods
self Keyword
Encapsulation
Public Members
Protected Members
Private Members
Inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
super() Function
Polymorphism
Method Overriding
Duck Typing
Abstraction
Abstract Class
Abstract Method
Class Method
Static Method
Special Methods
_str_ Method
Operator Overloading
Composition
Aggregation
Association
Method Resolution Order
Object Class
issubclass() and isinstance()
Property Decorator
Setter Method with @property
Destructor
Inner Class
Complete Mini Example
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
Executing...
❌ Error:
✅ Output:
// Click Run ▶ to execute
🏋️ Test Yourself With Exercises
Take our quiz on Aggregation to test your knowledge.
Browse Quizzes »