Inheritance in Python
Python inheritance and types of inheritance in python with their syntax. Moreover, we will study Python super function, Python method overriding and Python method overloading.
So, let’s start the Python Inheritance Tutorial.
It is a universal fact that every student is a person. This is in hindsight of non-human students if any. To depict this relationship, we take an illustration.
The relationship from Animal to the tiger is termed ‘Specialization’. Conversely, every Tiger is an animal, this is called Generalization. In this representation, we use an arrow towards the base class as a UML (Unified Modeling Language) convention.
Here, Animal can be called any of the following:
Likewise, Tiger here is:
Python Inheritance Syntax:
To make a class inherit from another, we apply the name of the base class in parentheses to the derived class’ definition.
Here, class Tiger inherits from the class Animal. Here, since we only want to focus on the python syntax, we use the ‘pass’ statement in the bodies of the classes. Also, we use the function issubclass() to confirm that Tiger is a subclass of Animal.
In Python, there are 5 types of Inheritance:
- Single inheritance
- Hybrid inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
Single Inheritance in Python
A single Python inheritance is when a single class inherits from a class.
Python Multiple Inheritance
Multiple Python inheritance are when multiple python classes inherit from a class.
Multilevel Inheritance in Python
When one class inherits from another, which in turn inherits from another, it is multilevel python inheritance.
Hierarchical Inheritance in Python
When more than one class inherits from a class, it is hierarchical Python inheritance.
Hybrid Inheritance in Python
Hybrid Python inheritance is a combination of any two kinds of inheritance.
Python Inheritance Super Function – Super()
With inheritance, the super() function in python actually comes in quite handy. It allows us to call a method from the parent class. Let’s define a new class for this.
Python Override Method
A subclass may change the functionality of a Python method in the superclass. It does so by redefining it. This is termed python method overriding. Lets see this Python Method Overriding Example.
Python Method Overloading
Before we say anything, we’d like you to take a look at the following code:
What looks like overloading methods, it is actually that Python keeps only the latest definition of a method you declare to it. This code doesn’t make a call to the version of add() that takes in two arguments to add. So we find it safe to say Python doesn’t support method overloading. However, we recently ran into a rather Pythonic way to make this happen. Check this out:
In this code, not only do we use the *args magic variable for variable arity, we also let the code deal with both integers and strings. Watch it happen:
To that, we’ll say this isn’t method overloading, this is simply used of default arguments.