Inheritance models an “is-a” relationship between classes. A subclass inherits the properties and behaviors of its parent class and can override or extend them.
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Bark"Composition models a “has-a” relationship by combining objects. A class is composed of other class instances and delegates behavior to them
class Engine:
def start(self):
return "Engine started"
class Car:
def __init__(self):
self.engine = Engine()
def start(self):
return self.engine.start()