Understanding composition and inheritance
Hey there, young coder! Today, we will learn about two important concepts in programming: composition and inheritance. Imagine you’re building with LEGO blocks. Sometimes you use different blocks together to create something cool (composition), and other times you use special blocks that inherit features from other blocks (inheritance). Let’s dive into these concepts with some fun examples using Python!
Composition
Composition is like putting together different LEGO blocks to build something new. In programming, it means creating complex objects by combining simpler ones.
Example
Let’s create a Car
by combining simpler objects like Engine
and Wheels
.
class Engine:
def __init__(self, horsepower):
self.horsepower = horsepower
def start(self):
return "Vroom! The engine starts."
class Wheels:
def __init__(self, count):
self.count = count
def roll(self):
return "The wheels roll smoothly."
class Car:
def __init__(self, horsepower, wheel_count):
self.engine = Engine(horsepower)
self.wheels = Wheels(wheel_count)
def drive(self):
return f"{self.engine.start()} {self.wheels.roll()}"
# Let's create a car!
my_car = Car(120, 4)
print(my_car.drive())
In this example:
- We have an
Engine
class with astart
method. - We have a
Wheels
class with aroll
method. - The
Car
class combines anEngine
andWheels
to create a more complex object.
Inheritance
Inheritance is like having a LEGO block that can do everything another block can do, plus more. In programming, it means creating a new class that takes all the features of an existing class and adds new ones.
Example
Let’s create a FlyingCar
that inherits from Car
and adds the ability to fly.
class Car:
def __init__(self, horsepower, wheel_count):
self.engine = Engine(horsepower)
self.wheels = Wheels(wheel_count)
def drive(self):
return f"{self.engine.start()} {self.wheels.roll()}"
class FlyingCar(Car):
def __init__(self, horsepower, wheel_count, wingspan):
super().__init__(horsepower, wheel_count)
self.wingspan = wingspan
def fly(self):
return f"The car spreads its wings and flies with a wingspan of {self.wingspan} meters."
# Let's create a flying car!
my_flying_car = FlyingCar(150, 4, 15)
print(my_flying_car.drive())
print(my_flying_car.fly())
In this example:
FlyingCar
inherits fromCar
.FlyingCar
has all the features ofCar
, plus a newfly
method.
Practice Questions
Now it’s your turn! Try these practice questions to reinforce your understanding.
Question 1
Create a House
class that uses composition to combine Door
and Window
classes.
class Door:
def __init__(self, material):
self.material = material
def open(self):
return "The door opens."
class Window:
def __init__(self, size):
self.size = size
def open(self):
return "The window opens."
class House:
# Your code here
Solution:
class Door:
def __init__(self, material):
self.material = material
def open(self):
return "The door opens."
class Window:
def __init__(self, size):
self.size = size
def open(self):
return "The window opens."
class House:
def __init__(self, door_material, window_size):
self.door = Door(door_material)
self.window = Window(window_size)
def enter(self):
return f"{self.door.open()} {self.window.open()}"
# Let's create a house!
my_house = House("wood", "large")
print(my_house.enter())
Question 2
Create a Smartphone
class that inherits from Phone
and adds a touchscreen
method.
class Phone:
def __init__(self, brand):
self.brand = brand
def call(self):
return "Calling..."
class Smartphone(Phone):
# Your code here
Solution:
class Phone:
def __init__(self, brand):
self.brand = brand
def call(self):
return "Calling..."
class Smartphone(Phone):
def __init__(self, brand, os):
super().__init__(brand)
self.os = os
def touchscreen(self):
return "Touchscreen activated."
# Let's create a smartphone!
my_smartphone = Smartphone("TechBrand", "Android")
print(my_smartphone.call())
print(my_smartphone.touchscreen())
Great job, young coder! You’ve learned the basics of composition and inheritance in Python. These concepts help you build more complex and reusable code, just like building with LEGO blocks. Keep practicing, and you’ll become a coding master in no time!