Composition means building complex behavior by combining simpler components, rather than inheriting from a base class.
“Has-a” relationship instead of “is-a.”
classEngine:defstart(self):print("Engine started")classCar:def__init__(self):self.engine =Engine()# Compositiondefstart(self):self.engine.start()print("Car is ready to go!")
Car "has-a" engine, not "is-a"
Instead of inheriting from Engine, the Car contains it.
This decouples the classes and makes reuse easier.
Polymorphism
Polymorphism allows you to use different objects interchangeably if they follow the same interface or behavior.
“Many forms” — the same interface can be implemented in multiple ways.
render() doesn't care what kind of Shape it's drawing.
As long as the object implements draw(), it works.
This enables extensibility without modification - supports Open-Closed Principle.
class Shape:
def draw(self):
raise NotImplementedError()
class Circle(Shape):
def draw(self):
print("Drawing a circle")
class Square(Shape):
def draw(self):
print("Drawing a square")
def render(shape: Shape):
shape.draw()
# Polymorphic use
render(Circle()) # Drawing a circle
render(Square()) # Drawing a square