Composition over inheritance

Inheritance says "is a". Composition says "has a": the object keeps another object in an attribute and calls it.

Suppose you want a stack. class BadStack(list) works, but it inherits every list method. Callers can insert in the middle or sort it, and the class can't stop them.

Stack holds a list in self._items and exposes only push, pop and len. It's smaller, safer, and you can swap the list for something else later without breaking callers.

The same idea helps testing. A class that receives its database or clock as an argument can be handed a fake one in a test. In System design interviews, say "has a" components that you can replace.

Rule of thumb: inherit only when the subclass really is a kind of the parent and needs all of its behaviour.

Run it, then try good.insert(0, 99).