liveBook · Manning

Superclass

As explained in Python Workout, Second Edition

A parent class is a Python class from which a child class or subclass inherits attributes and behavior.

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

How Python Workout, Second Edition covers it

How deep it goes
Explains declaration, inherited attribute lookup, and initializer reuse with super(), but stops short of advanced inheritance variations.
Examples
Uses Foo and Bar, Person and Employee, and class families such as Vehicle, Car, Truck, and Bicycle.
What it assumes
Assumes familiarity with Python classes, instances, methods, and initializers while explaining inheritance and parent lookup directly.
What you can do after
Apply it via Python class syntax to reuse parent behavior, add subclass features, and delegate initialization with super().
How it connects
Connects superclasses with subclasses, inheritance, attribute lookup, methods, class attributes, and object-oriented design.

Also explained in

Compare how 10 books explain superclass →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.