Quantcast
Channel: Python
Viewing all articles
Browse latest Browse all 146

Multiple Inheritance

$
0
0

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.

python inheritance

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

classSub(Super):
pass

Basic inheritance is done in this way in Python:

# Class
classA:
defa(self):
print('a')

# Class B inherits from Class A
classB(A):
defb(self):
print('b')

# Create object
obj = B()
B.a()
B.b()

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

classParent1:
pass

classParent2:
pass

classParent3:
pass

classKid1(Parent1, Parent2, Parent3):
pass

These classes don’t have an implementation, but they show how to use multiple inheritance.

multiple inheritance example

To make a class inehrit from classes, just add them after the parenthesis.

classKid1(Parent1):
classKid1(Parent1, Parent2):
classKid1(Parent1, Parent2, Parent3):
classKid1(Parent1, Parent2, Parent3, Parent4):
classKid1(Parent1, Parent2, Parent3, Parent4, Parent5):

Lets add some variables to the classes:

>>> classParent1:
... x = 1
...
>>> classParent2:
... y = 1
...
>>> classParent3:
... z = 1
...
>>> classKid1(Parent1, Parent2, Parent3):
... u = 2
...

Create a new object

>>> k = Kid1()

All variables are now available:

>>> k.x
1
>>> k.y
1
>>> k.z
1
>>> k.u
2
>>>

If you are a Python beginner, then I highly recommend this book.

Multiple Inheritance Example

Take a look at this multiple inheritance example:

>>> classWorker:
... title = ''
... pay = ''
...
>>> classTeamMember:
... project = ''
...
>>> classTeamLeader(TeamMember, Worker):
... experience = ''

You can create an object with every class.

Which variables will be available, depends on the class.

multiple inheritance example with two classes

The first two classes don’t inherit, so only those variables are available:

>>> bob = Worker()
>>> bob.
bob.pay bob.title

>>> jim = TeamMember()
>>> jim.project

If you create an object using class TeamLeader (multiple inheritance), it has many variables available.

>>> trevor = TeamLeader()
>>> trevor.
trevor.experience trevor.pay trevor.project trevor.title

Methods can be inherited too, the class TeamLeader below inherits all methods.

>>> classWorker:
... title = ''
... pay = ''
... defsetTitle(self, title):
... self.title = title
... defsetPay(self, pay):
... self.pay = pay
...
>>> classTeamMember:
... project = ''
... defsetProject(self, project):
... self.project = project
...
>>> classTeamLeader(TeamMember, Worker):
... experience = ''
...
>>> jim = TeamLeader()
>>> jim.setTitle("Team Lead")
>>> jim.setPay(900000)
>>> jim.setProject("Python Zen")

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()

>>> isinstance(obj, Car)
False
>>> isinstance(obj, Boat)
True

The method issubclass() tests if a class inherits from a class or not.

>>> classVehicle:
... pass
...
>>> classCar(Vehicle):
... pass
...

>>> issubclass(Vehicle, Car)
False
>>> issubclass(Car, Vehicle)
True

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.

>>> classGrandpa:
... x = 1
...
>>> classFather(Grandpa):
... y = 2
...
>>> classKid(Father):
... z = 3
...
>>> k = Kid()
>>> f"{k.x}{k.y}{k.z}"
'1 2 3'

Here each class only inherits once, at most, but they inherit as a series.

multi-level inheritance

These two classes inherit once:

>>> classFather(Grandpa):
>>> classKid(Father):

That’s why we call this multi-level inheritance.

The example below shows multi-level inheritance in a different context.

# Grandfather
>>> classVehicle:
... engine = True

# Parent
>>> classAutoMobile(Vehicle):
... wheels = 4

# Children
>>> classCar(AutoMobile):
... seats = 4
...
>>> classTruck(AutoMobile):
... seats = 2
...

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.

Download


Viewing all articles
Browse latest Browse all 146

Trending Articles