A superclass, or parent class, defines general attributes and behavior that a subclass inherits. This keeps similar classes organized and reduces repeated code. section 9.15
Use a superclass when:
- Several classes share core behavior but need specialized additions, such as Person and Employee or Vehicle and Car. section 9.15
- A subclass should reuse or extend parent methods without changing the parent. section 9.16
- A child initializer needs parent setup; call super().__init__(...) before adding child-specific attributes. section 9.15
A subclass reuses parent behavior
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()) # 20 print(b.x3()) # 30 Python searches an attribute on the instance, its class, then parent classes and object. section 9.10
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
Runnable inheritance example showing a child instance using both inherited and newly defined methods. section 9.10









