In Python a class can inherit from more than one class.
If a class inherits, it has the methods and variables from the parent classes.
In essence, it’s called multiple inheritance because a class can inherit from multiple classes. This is a concept from object orientated programming.
If you are totally new to (object orientated) programming, I recommend the course below.
Related course: Python Programming Courses & Exercises
Introduction
Inheritance is if a sub class gets all the methods and variables from a super class.
The inherited methods and variables can then be used in a newly created object from the sub class.
In the above image, class Student inherits from class Person.
So the method introduce()
and variables firstname
and lastname
will be inside class Student.
If you create an object from the class Student, you can access both variables and methods from Student and Person.
In multiple inheritance, a class gets all the variables and methods from more than one parents.
Inheritance
To make a class inherit from a super class, you have to define the parenthesis in the class definition
class Sub(Super): |
Basic inheritance is done in this way in Python:
# Class |
Now let’s investigate multiple inheritance, inheriting from multiple classes.
Multiple inheritance
So what is multiple inheritance?
In multiple inheritance, a class inherits from two or more super classes.
It inherits the methods and variables from all super classes.
If you create an object, it has all methods and variables from the classes.
Lets see an example, where a class inherits from three classes
class Parent1: |
These classes don’t have an implementation, but they show how to use multiple inheritance.
To make a class inehrit from classes, just add them after the parenthesis.
class Kid1(Parent1): |
Lets add some variables to the classes:
class Parent1: |
Create a new object
k = Kid1() |
All variables are now available:
k.x |
If you are a Python beginner, then I highly recommend this book.
Multiple Inheritance Example
Take a look at this multiple inheritance example:
class Worker: |
You can create an object with every class.
Which variables will be available, depends on the class.
The first two classes don’t inherit, so only those variables are available:
bob = Worker() |
If you create an object using class TeamLeader (multiple inheritance), it has many variables available.
trevor = TeamLeader() |
Methods can be inherited too, the class TeamLeader below inherits all methods.
class Worker: |
Built-in inheritance methods in Python
Python has a few methods that help with inheritance.
The method isinstance() returns a boolean, telling you if an object is an instance of a class.
obj = Boat() |
The method issubclass() tests if a class inherits from a class or not.
class Vehicle: |
If you are a Python beginner, then I highly recommend this book.
Multi-level inheritance
You may see multi-level inheritance, this is not multiple inheritance.
In multi-level there are several levels, which create an inheritance relationship.
This is similar to the relationship between grandpa, father and child.
class Grandpa: |
Here each class only inherits once, at most, but they inherit as a series.
These two classes inherit once:
class Father(Grandpa): |
That’s why we call this multi-level inheritance.
The example below shows multi-level inheritance in a different context.
# Grandfather |
Multiple Inheritance vs Multi-Level inheritance
There are a few key differences between multiple inheritance and multi-level inheritance.
Multiple inheritance is when there are multiple super classes
Multiple inheritance can get complex, so not that widely used. Imagine six class inheritance, which variables and methods are available? You quickly lose the overview.
Multiple inheritance requires a two classs hierarchy: child and parents
Multi-level inheritance requires at least three levels of classes: grandfather, father and sons
If you are a Python beginner, then I highly recommend this book.