Inheritance lets a new Python class reuse the useful parts of an existing class and then add or change behavior of its own.
In OSPython.012: Classes and Objects, we created classes as blueprints. In this lesson, we connect two blueprints: a general parent class and a more specific child class.
Start with a parent class
class Vehicle:
def move(self):
return "Moving"
Vehicle is a simple general class. It has one method named move().
Create a child class
class Car(Vehicle):
pass
my_car = Car()
print(my_car.move())
The output is Moving. We never wrote move() inside Car. Python found it in the parent Vehicle class. That reuse is the basic idea of inheritance.
Parent and child in plain English
- Parent class: the more general class that provides reusable attributes or methods.
- Child class: a more specific class that inherits from the parent.
- Inheritance: the relationship that lets the child reuse the parent’s behavior.
A familiar example is Vehicle → Car. A car is a kind of vehicle, so a Car class can reuse general vehicle behavior while adding car-specific behavior.
Add something new to the child
class Vehicle:
def move(self):
return "Moving"
class Car(Vehicle):
def honk(self):
return "Beep!"
my_car = Car()
print(my_car.move())
print(my_car.honk())
my_car can use move() from its parent and honk() from its own class.
Video: Creating Python subclasses
Override a parent method
A child does not have to keep every inherited behavior exactly the same. If the child defines a method with the same name, Python uses the child’s version for that child object.
class Vehicle:
def sound(self):
return "Vehicle sound"
class Car(Vehicle):
def sound(self):
return "Beep!"
my_car = Car()
print(my_car.sound())
The output is Beep!. This is called overriding: Car supplies its own version of sound().
Gaming example
class Player:
def move(self):
return "Player moves"
class Runner(Player):
def sprint(self):
return "Runner sprints"
character = Runner()
print(character.move())
print(character.sprint())
A game can place behavior shared by many characters in Player, then put special behavior in classes such as Runner. This avoids rewriting the same basic action in every class.
Simple Bitcoin mining example
class Machine:
def power_on(self):
return "Power on"
class Miner(Machine):
def start_mining(self):
return "Mining started"
unit = Miner()
print(unit.power_on())
print(unit.start_mining())
A miner is a kind of machine. The child class can reuse a general machine action and add a mining-specific action. Keep the example simple: inheritance is the new concept here.
Inheritance with __init__()
OSPython.012 introduced __init__() and self. A child class can reuse the parent’s initializer with super().
class Player:
def __init__(self, name):
self.name = name
class Runner(Player):
def __init__(self, name, speed):
super().__init__(name)
self.speed = speed
player = Runner("Alex", 10)
print(player.name)
print(player.speed)
super().__init__(name) asks the parent class to handle the part it already knows how to set up. The child then adds speed.
When inheritance makes sense
Use inheritance when the child really is a kind of the parent: a car is a vehicle, a runner is a player, or a miner is a machine. Do not force unrelated things into an inheritance relationship just to reuse a few lines of code.
Common beginner mistakes
- Forgetting to put the parent class inside the child’s parentheses.
- Expecting a parent object to automatically gain methods that exist only on a child.
- Overriding a method accidentally by reusing the same method name.
- Forgetting
super()when the child needs the parent’s initialization work. - Building a complicated class tree before the basic relationship is clear.
Practice
- Create a parent class named
Animalwith a method namedmove(). - Create a child class named
Dogthat inherits fromAnimal. - Create a
Dogobject and call the inheritedmove()method. - Add a new
bark()method toDog. - Override
move()insideDogand print the new result.
Key takeaway
Inheritance lets a child class reuse behavior from a parent class. Start with a clear parent-child relationship, inherit what is shared, add what is unique, and override behavior only when the child genuinely needs a different version. The class fundamentals from OSPython.012 are the foundation for everything in this lesson.

Leave a comment