Module 4 Lesson 8: Composition vs. Inheritance
·Programming

Module 4 Lesson 8: Composition vs. Inheritance

Choosing the right way to build complex systems. Learn the 'is-a' vs. 'has-a' rule and why Composition is often better than Inheritance.

Module 4 Lesson 8: Composition vs. Inheritance

Inheritance is great for sharing code between a "Type" and its "Category" (A Dog is an Animal). But sometimes, using inheritance makes your code too tangled and rigid. What if an object doesn't inherit traits but simply uses other objects? This is called Composition.

Lesson Overview

In this lesson, we will cover:

  • The "is-a" vs. "has-a" Rule: The ultimate design test.
  • How Composition Works: Objects as attributes.
  • Pros and Cons: When to use which.
  • Practical Example: Building a Computer.

1. The Golden Rule

Before you write class B(A):, ask yourself:

  • Inheritance (is-a): Is a Dog a kind of Animal? Yes! → Use Inheritance.
  • Composition (has-a): Does a Computer have a kind of CPU? Yes! Is a Computer a kind of CPU? No! → Use Composition.

2. How Composition Works

In composition, you create an object and then pass it as an argument into another object’s constructor.

class Engine:
    def start(self): print("Engine starting...")

class Car:
    def __init__(self, brand):
        self.brand = brand
        self.engine = Engine() # The Car HAS AN Engine (Composition)

    def drive(self):
        self.engine.start()
        print(f"The {self.brand} is moving!")

my_car = Car("Toyota")
my_car.drive()

3. Why Composition is Often Better

  1. Flexibility: You can swap out a GasEngine for an ElectricEngine easily. With inheritance, you'd have to create two entirely different Car classes (GasCar and ElectricCar).
  2. Decoupling: Parts can be changed or tested separately without breaking the whole system.
  3. No "Deep Hierarchies": You won't end up with a confusing Vehicle -> LandVehicle -> WheeledVehicle -> Car -> SportsCar chain.

4. When to Use Each

  • Inheritance: Use it when class B really is a specialized version of class A, and they share almost all behavior.
  • Composition: Use it when you are building a complex object out of smaller, independent parts.

Practice Exercise: The Robot Builder

Create a file named robot.py.

  1. Define a class Arm with a method grasp().
  2. Define a class Leg with a method step().
  3. Define a class Robot.
  4. In the Robot's __init__, create an instance of Arm and an instance of Leg.
  5. Add a method move_and_pick() to Robot that calls the leg's step() and then the arm's grasp().

Quick Knowledge Check

  1. What is the "is-a" vs. "has-a" rule?
  2. In a "Bank" application, would a Customer inherit from a Bank or would a Bank use composition for its Customer?
  3. Give one advantage of Composition over Inheritance.
  4. If a class Bird inherits from Animal, what kind of relationship is that?

Key Takeaways

  • Inheritance models relationship ("is-a").
  • Composition models structural components ("has-a").
  • Composition leads to more flexible and easier-to-change code.
  • The best software usually uses a mix of both!

What’s Next?

Our methods so far always need an instance (my_car.drive()). But what if a method belongs to the Class itself (like Car.get_count())? In Lesson 9, we’ll look at Class and Static Methods!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn