What is the difference between composition and inheritance?

Python classes and OOP · Freshers and experienced

Model answer

Inheritance means a class is a kind of another class. Composition means a class has other objects as parts (an Order has a list of Items). Composition is usually more flexible and easier to change, which is why "favour composition over inheritance" is common advice.

Learn this topic

Classes and Objects in Python: A Gentle Introduction in the course Python for Beginners: Learn to Code with Real Projects.

Sign in to add your own answer.

More questions