Inheritance in Python

This is the 3rd article in a series on Object-Oriented Programming:


Let’s define a generic Vehicle class and save it inside the vehicle.py file.

class Vehicle:
    def __init__(self, year, model, plate_number, current_speed):
        self.year = year
        self.model = model
        self.plate_number = plate_number
        self.current_speed = current_speed

    def move(self):
        self.current_speed += 1

    def accelerate(self, value):
        self.current_speed += value

    def stop(self):
        self.current_speed = 0

    def vehicle_details(self):
        return 'Model: ' + self.model + ', Year: ' + str(self.year) + ', Plate: ' + self.plate_number

A vehicle has attributes year, model, plate_number, and current_speed.

The definition of vehicle in the Vehicle is very generic and might not be suitable for trucks for instance because it should include a cargo attribute.

On the other hand, a cargo attribute does not make much sense for small vehicles like motorcycles.

To solve this we can use inheritance.

When a class (child) inherits another class (parent), all the attributes and methods from to parent class are inherited by the child class.

Parent and Child

In our case, we want a new Truck class to inherit everything from the Vehicle class and add its own specific attribute current_cargo to control the addition and removal of cargo from the truck.

The Truck class is called a child class that inherits from its parent class Vehicle.

A parent class is also called a superclass while a child class is also known as a subclass.

Create the class Truck and save it inside the truck.py file.

from vehicle import Vehicle

class Truck(Vehicle):
    def __init__(self, year, model, plate_number, current_speed, current_cargo):
    super().__init__(year, model, plate_number, current_speed)
    self.current_cargo = current_cargo

    def add_cargo(self, cargo):
        self.current_cargo += cargo

    def remove_cargo(self, cargo):
        self.current_cargo -= cargo

Let’s break down the class to explain it in parts.

The class Vehicle inside the parentheses when defining the class Truck indicates that the parent Vehicle is being inherited by its child Truck.

The init method has self as its first parameter, as usual.

The parameters year, model, plate_number, and current_speed are there to match the ones in the Vehicle class.

We added a new parameter current_cargo suited for the Truck class.

In the first line of the init method of the Truck class we have to call the init method of the Vehicle class.

To do that we use super() to make a reference to the supperclass Vehicle, so when super().init(year, model, plate_number, current_speed) is called we avoid repetition of code.

After that, we can assign the value of current_cargo normally.

Finally, we have two methods to deal with the current_cargo: def add_cargo(self, cargo):, def remove_cargo(self, cargo):.

Remember that Truck inherits attributes and methods from Vehicle, so we also have an implicit access to the methods that manipulate the speed: def move(self), def accelerate(self, value), def stop(self).

Using the Truck class

Use the class on a terminal, import the Truck class from the truck module.

Create an instance called my_truck, initializing year with 2015, model with ‘V8’, plate_number with ‘XYZ1234’, current_speed with 0, and current_cargo with 0.

Use add_cargo(10) to increase current_cargo by 10, remove_cargo(4), to decrease current_cargo by 4.

Remember to print the value of current_cargo at every command to see the changes.

By inheritance, we can use the methods from the Vehicle class to move() the truck which increases its current_speed by 1, accelerate(10) which increases its current_speed by the value given in the argument, and stop() which sets the current_speed to 0.

Remember to print the value of current_speed at every interaction to see the changes.

To finish the test, call vehicle_details() inherited from the Vehicle class to print the information about our truck.

>>> from truck import Truck
>>> 
>>> my_truck = Truck(2015, 'V8', 'XYZ1234', 0, 0)
>>> print(my_truck.current_cargo)
0
>>> my_truck.add_cargo(10)
>>> print(my_truck.current_cargo)
10
>>> my_truck.remove_cargo(4)
>>> print(my_truck.current_cargo)
6
>>> print(my_truck.current_speed)
0
>>> my_truck.accelerate(10)
>>> print(my_truck.current_speed)
10
>>> my_truck.stop()
>>> print(my_truck.current_speed)
0
>>> print(my_truck.vehicle_details())
Model: V8, Year: 2015, Plate: XYZ1234