Composition: “has-a” relationship
What is Composition?
Imagine you have a toy car. This toy car has different parts: wheels, a body, an engine, and so on. The toy car has wheels, it has a body, and it has an engine. This is exactly what composition is in programming. It means that one object is made up of other objects.
We can use classes to represent this “has-a” relationship. Let’s look at how this works with a simple example.
Example: Building a Toy Car
Step 1: Creating the Parts
First, let’s create the different parts of our toy car.
- Wheels
- Body
- Engine
We will create a class for each part.
class Wheel:
def __init__(self, brand):
self.brand = brand
class Body:
def __init__(self, color):
self.color = color
class Engine:
def __init__(self, horsepower):
self.horsepower = horsepower
Each part has a specific attribute:
- Wheel has a
brand
. - Body has a
color
. - Engine has
horsepower
.
Step 2: Creating the Toy Car
Now, let’s create the Car
class that has these parts.
class Car:
def __init__(self, brand, color, horsepower):
self.wheel = Wheel(brand)
self.body = Body(color)
self.engine = Engine(horsepower)
def display_info(self):
print(f"Car with {self.wheel.brand} wheels, {self.body.color} body, and {self.engine.horsepower} horsepower engine.")
The Car
class has:
- A
Wheel
object. - A
Body
object. - An
Engine
object.
It also has a method display_info
to print out the details of the car.
Step 3: Creating and Using the Toy Car
Now, let’s create a toy car and see how it works.
my_toy_car = Car(brand="Goodyear", color="red", horsepower=300)
my_toy_car.display_info()
When you run this code, it will output:
Car with Goodyear wheels, red body, and 300 horsepower engine.
Practice Questions
Question 1: Adding Seats to the Car
Create a Seat
class with an attribute material
. Then update the Car
class to include seats.
# Your code here
Question 2: Creating Multiple Cars
Create a list of cars with different brands, colors, and horsepower. Then, display the information for each car.
# Your code here
Solutions
Solution 1: Adding Seats to the Car
class Seat:
def __init__(self, material):
self.material = material
class Car:
def __init__(self, brand, color, horsepower, seat_material):
self.wheel = Wheel(brand)
self.body = Body(color)
self.engine = Engine(horsepower)
self.seat = Seat(seat_material)
def display_info(self):
print(f"Car with {self.wheel.brand} wheels, {self.body.color} body, {self.engine.horsepower} horsepower engine, and {self.seat.material} seats.")
my_toy_car = Car(brand="Goodyear", color="red", horsepower=300, seat_material="leather")
my_toy_car.display_info()
When you run this code, it will output:
Car with Goodyear wheels, red body, 300 horsepower engine, and leather seats.
Solution 2: Creating Multiple Cars
cars = [
Car(brand="Goodyear", color="red", horsepower=300, seat_material="leather"),
Car(brand="Michelin", color="blue", horsepower=250, seat_material="fabric"),
Car(brand="Pirelli", color="green", horsepower=400, seat_material="suede")
]
for car in cars:
car.display_info()
When you run this code, it will output:
Car with Goodyear wheels, red body, 300 horsepower engine, and leather seats.
Car with Michelin wheels, blue body, 250 horsepower engine, and fabric seats.
Car with Pirelli wheels, green body, 400 horsepower engine, and suede seats.
In this lesson, we learned about composition, and the “has-a” relationship in Python. We saw how we can create a complex object (a car) from simpler objects (wheels, body, engine, seats). Composition helps us build and manage more complex systems by breaking them down into smaller, manageable parts.
By practicing these concepts, you’ll get better at structuring your code and making it more modular and easier to understand. Happy coding!