A superclass, also called a parent class, defines general attributes and behavior for one or more child classes, or subclasses. A subclass inherits what the superclass provides and can add more specific functionality. In Python, declare the relationship by placing the parent class in parentheses after the subclass name. section 10.16
Attribute lookup makes inheritance work naturally. Python checks the instance, then its class, then the class’s parents. If a subclass does not define a method such as greet, Python can find and invoke the inherited implementation on the superclass. section 10.12 section 10.16
Extending a person class
class Person: def __init__(self, name): self.name = name def greet(self): return f'Hello, {self.name}' class Employee(Person): def __init__(self, name, id_number): super().__init__(name) self.id_number = id_number Employee inherits greet from Person while adding an identifier. Calling Employee('Ava', 1).greet() uses the superclass implementation. Calling super().__init__(name) reuses the parent initializer instead of duplicating its assignment. section 10.16
Use a superclass when:
- Several related classes share general behavior, such as Person with Employee or Vehicle with Car, Truck, and Bicycle. section 10.16
- A specialized class should extend an existing implementation while avoiding repeated code and keeping related classes organized. section 10.16
- A child initializer needs parent setup; use super().__init__(...) rather than repeating the parent’s initialization logic. section 10.16
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
The compact Foo–Bar example shows a Bar instance using x2 from its superclass and x3 from its own class. section 10.12
class Employee(Person):
def __init__(self, name, id_number):
super().__init__(name) #1
self.id_number = id_number
The Employee example demonstrates delegating initialization to the Person superclass with super().__init__(name). section 10.16









