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): |
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 |
Then you can create a child class.
The child class will “copy” the variables and methods of the parent.
# Child class |
Then if you create an object using class Student, it will have the parents variables and methods.
obj = Student() |
The child class can have its own methods and variables.
# Child class |
Then your object can use both methods and variables from the child and parent class:
obj = Student() |
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: |
Then we create class B and let it inherit from class A.class B(A):
def B(self):
print('B')
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: |
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() |
You can add new methods and variables into your Student class, that will also be available:
class Student(User): |
So you can access both the methods and variables from the sub class and the super class.
s = Student() |
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:
Back to inheritance with constructors.
If you have two classes with two constructors:
class Person: |
Then the childs constructor will be used, so if you try the parents constructor it throws this error:
"Alice","Allison") obj = Student( |
But using the childs constructor works just fine:
357) obj = Student( |
If no constructor is defined in the childs class, the parents class constructor is used.
class Person: |
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: |
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 |
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.