Inheritance lets a subclass reuse and extend a parent class. Use it when the relationship is “is-a”: an Employee is a Person, a Car is a Vehicle, or a BigBowl is a Bowl. Do not confuse this with composition, where one object contains another: a Bowl has-a Scoop. section 10.10 section 10.16
Define the parent class in parentheses after the subclass name. Python searches for attributes through the instance, its class, the class’s parents, and ultimately object. A subclass therefore receives inherited methods and can replace a method by defining an attribute with the same name. section 10.12 section 10.6
Extend a class with minimal code
A Bowl can enforce a three-scoop limit while BigBowl changes only the capacity. Store the limit as a class attribute and let the inherited method read self.max_scoops. The same method then uses 3 for Bowl instances and 5 for BigBowl instances. section 10.17 section 10.18
class Foo():
def __init__(self, x):
self.x = x
def x2(self):
return self.x * 2
class Bar(Foo):
def x3(self):
return self.x * 3
b = Bar(10)
print(b.x2()) #1
print(b.x3()) #2
Shows a Bar instance receiving __init__ and x2 from Foo while defining its own x3 method. section 10.12
When a subclass defines its own __init__, call super().__init__ to reuse the parent initialization, then assign subclass-specific attributes. This avoids duplicating parent setup and keeps the hierarchy easier to maintain. section 10.16 section 10.26
Practice inheritance by deciding what belongs in the shared parent and what belongs in each subclass:
- Create Animal with shared representation and initialization, then derive Wolf, Sheep, Snake, and Parrot with species-specific details. section 10.25 section 10.26
- Extend dict with FlexibleDict, which retries missing keys after converting them to str and int. section 10.21
- Design Envelope and BigEnvelope, or Phone, SmartPhone, and iPhone, to practice inherited behavior and selective overrides. section 10.20
Inheritance reduces repeated code and clarifies relationships among similar types, but excessive inheritance can create unwieldy designs. Compare the is-a relationship with composition before choosing the technique. section 10.2


















