Inheritance is one of the key concepts of object orientated programming (OOP). Objects are created using classes, but that’s not all.

A class can inherit the methods and variables from another class. This principle helps us to avoid duplicate code. In turn, a created object has methods and variables from both classes.

Related course: Python Programming Courses & Exercises

Inheritance introduction

Inheritance a concept of object orientated programming and requires classes.

Inheritance is the ability from one class to inherit from a parent class.

Inheritance at minimum requires two classes

  • Parent class is the class that is being inherited from

  • Child class a class that inherits from the parent class

In contrast to real world inheritance, in object oriented programming

A child class inherits methods and variables from the parent class.

Syntax

The syntax for inheritance in Python is:

class ChildClassName(ParentClassName):
pass

Any class can be a Parent and any class can be a Child.

Instead of the pass statement, you can put the child class variables and metods there.

The parents class variables and methods will be added to the child class.

Inheritance example

Any class can be a parent class, and as such you just create a class.

The class can have variables, methods or both.

The parent class we create is:

# Parent class
class Person:
firstname = ''
lastname = ''

def introduce(self):
print("My name is " + self.firstname + " " + self.lastname)

Then you can create a child class.

The child class will “copy” the variables and methods of the parent.

# Child class
class Student(Person):
pass

Then if you create an object using class Student, it will have the parents variables and methods.

>>> obj = Student()
>>> obj.firstname = "Alice"
>>> obj.lastname = "Allison"
>>> obj.introduce()
My name is Alice Allison
>>>

The child class can have its own methods and variables.

>>> # Child class
>>> class Student(Person):
... school = "Harvard"
... study = "Computer Science"
... year = 1
... def education(self):
... print("I am studying " + self.study + " at " + self.school)
...

python inheritance

Then your object can use both methods and variables from the child and parent class:

>>> obj = Student()
>>> obj.firstname = "Billy"
>>> obj.lastname = "Watson"
>>> obj.introduce()
My name is Billy Watson
>>> obj.education()
I am studying Computer Science at Harvard
>>>

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

Abstract example

Before starting with inheritance, create a class. We will require two classes, one parent class and one class that inherits.

Start by defining a class A which has a method A.

class A:
def A(self):
print('A')

Then we create class B and let it inherit from class A.

class B(A):
def B(self):
print('B')

python child class inherits from super class, inheritance

If you create an object from class B, it has both the methods defined in class A and class B.

In this example there’s only the constructor method, no other methods. But in the case A has several methods, they will be “copied into” B.

Full inheritance code:

class A:
def A(self):
print('A')

def GrandpaMethod(self):
print('Made in class A')

class B(A):
def B(self):
print('B')

o = B()
o.A()
o.B()
o.GrandpaMethod()

Real world example

The above example is very abstract, to make it more clear we create a concrete example.

Create a class User, that has some variables and methods.

class User:
name = ""

def getName(self):
return self.name

Turn that into a super class (= parent class), by defining the sub class (= child class) student.

class Student(User):
...

The class Student now inherits from the class User, meaning it will have the variable name and the method getName().

That mean that if you create a new object, you can access the method

s = Student()
print( s.getName() )

You can add new methods and variables into your Student class, that will also be available:

class Student(User):
studentID = 0

getStudentID():
return self.studentID

python inheritance class

So you can access both the methods and variables from the sub class and the super class.

s = Student()
print( s.getName() )
id = s.getStudentID()

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

The init function

The childs class overrides the parents constructor (__init__).

Recall that a constructor was the first method called on object creation and always named __init__.

So the higlighted methods are constructors:

python constructors

Back to inheritance with constructors.

If you have two classes with two constructors:

>>> class Person:
... def __init__(self, fname, lname):
... self.fname = fname
... self.lname = lname
...
>>> class Student(Person):
... def __init__(self, id):
... self.id = id
...

Then the childs constructor will be used, so if you try the parents constructor it throws this error:

>>> obj = Student("Alice","Allison")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes 2 positional arguments but 3 were given
>>>

But using the childs constructor works just fine:

>>> obj = Student(357)

If no constructor is defined in the childs class, the parents class constructor is used.

>>> class Person:
... def __init__(self, fname, lname):
... self.fname = fname
... self.lname = lname
...
>>> class Student(Person):
... pass
...
>>> obj = Student("Alice","Allison")

The super() function

The super() function returns a temporary object of the super class.

A super class is a parent class (see terminology below).

You can use that object to access the parents class methods and variabels.

The code below shows how to use the super() function.

>>> class Person:
... def walk(self):
... print('Person walks')
...
>>> class Student(Person):
... def gotoClass(self):
... super().walk()
...
>>> obj = Student()
>>> obj.gotoClass()
Person walks
>>>

The call super().walk() calls the parents class method.

Multiple inheritance

A Python class can inherit from more than one class.

To add multiple parent classes, you can add them separated by a comma:

>>> # Parent classes
>>> class Dad:
... pass
...
>>> class Mom:
... pass
...
>>> # Child class that inherits
>>> class Child(Mom,Dad):
... pass
...
>>>

Unlike nature, a class in Python can have one, three, four or more parent classes.

Terminology

The terminology has several synoyms.

The parent class is sometimes named the super class or base class.

The child class is sometimes named sub class.

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

Download exercises