Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
In this Python tutorial, we talk about Python inheritance and types of inheritance in Python with their syntax.
Moreover, we will study Python super function, Python method overriding, and method overloading in Python.
So, let’s start the Python Inheritance Tutorial.
What is Inheritance in Python?
Inheritance lets one class borrow from another. You write class Puppy(Dog): to say, “Puppy is a kind of Dog.” The new class gains all methods and data of Dog without rewriting them.
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 person to a student is termed ‘Specialization’. Conversely, every student is a person; this is called Generalization.
In this representation, we use an arrow towards the base class as a UML (Unified Modeling Language) convention.
Here, Person can be called any of the following:
- Super Class
- Parent Class
- Base Class
Likewise, the student here is:
- Sub Class
- Child Class
- Derived Class
Features of inheritance in Python:
- Namespace behaviour: If a method is defined again with the same name, then it will replace the old one.
- API Flexibility: It can create functions that can easily handle different types of tasks.
Python Inheritance Syntax
To make a class inherit from another, we apply the name of the base class in parentheses to the derived class’s definition.
>>> class Person: pass >>> class Student(Person): pass >>> issubclass(Student,Person)
Output
Here, class Student inherits from class Person. 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 the student is a subclass of person.
Types of Inheritance in Python
There are five types of inheritance in Python, we observe.
1. Single Inheritance in Python
A single Python inheritance is when a single class inherits from another class.
>>> x=0
>>> class fruit:
def __init__(self):
global x
x+=1
print("I'm a fruit")
>>> class citrus(fruit):
def __init__(self):
super().__init__()
global x
x+=2
print("I'm citrus")
>>> x
>>> lime=citrus()
Output
I’m citrus
>>> x
Output
3
2. Python Multiple Inheritance
Multiple inheritance in Python occurs when a class inherits from multiple base classes.
>>> class Color: pass >>> class Fruit: pass >>> class Orange(Color,Fruit): pass >>> issubclass(Orange,Color) and issubclass(Orange,Fruit)
Output
True
3. Multilevel Inheritance in Python
When one class inherits from another, which in turn inherits from another, it is multilevel Python inheritance.
>>> class A: x=1 >>> class B(A): pass >>> class C(B): pass >>> cobj=C() >>> cobj.x
Output
1
4. Hierarchical Inheritance in Python
When more than one class inherits from a class, it is hierarchical Python inheritance.
>>> class A: pass >>> class B(A): pass >>> class C(A): pass >>> issubclass(B,A) and issubclass(C,A)
Output
True
5. Hybrid Inheritance in Python
Hybrid Python inheritance is a combination of any two kinds of inheritance.
>>> class A: x=1 >>> class B(A): pass >>> class C(A): pass >>> class D(B,C): pass >>> dobj=D() >>> dobj.x
Output
1
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.
>>> class Vehicle:
def start(self):
print("Starting engine")
def stop(self):
print("Stopping engine")
>>> class TwoWheeler(Vehicle):
def say(self):
super().start()
print("I have two wheels")
super().stop()
>>> Pulsar=TwoWheeler()
>>> Pulsar.say()
Output
I have two wheels
Stopping engine
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. Let’s see this Python Method Overriding Example.
>>> class A:
def sayhi(self):
print("I'm in A")
>>> class B(A):
def sayhi(self):
print("I'm in B")
>>> bobj=B()
>>> bobj.sayhi()
Output
I’m in B
Python Method Overloading
Before we say anything, we’d like you to take a look at the following code:
>>> def add(a,b):
return a+b
>>> def add(a,b,c):
return a+b+c
>>> add(2,3)
Output
File “<pyshell#8>”, line 1, in <module>
add(2,3)
TypeError: add() missing 1 required positional argument: ‘c’
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:
>>> def add(instanceOf,*args):
if instanceOf=='int':
result=0
if instanceOf=='str':
result=''
for i in args:
result+=i
return result
In this code, not only do we use the *args magic variable for variable arity, but we also let the code deal with both integers and strings.
Watch it happen:
>>> add('int',3,4,5)
Output
12
>>> add('str','I ','speak ','Python')
Output
‘I speak Python’
You say what if I do this?:
>>> def add(a,b,c=0):
Output
return a+b+c
>>> add(2,3)
Output
5
To that, we’ll say this isn’t method overloading, this is simply the use of default arguments.
Python Interview Questions on Inheritance
1. How does inheritance work in Python?
2. What are the types of inheritance in Python?
3. Does Python support multiple inheritance?
4. What is Python inheritance? Explain with an example.
5. Is hierarchical inheritance possible in Python?
Conclusion
Of all things good, Python inheritance saves us time, effort, and memory.
In this tutorial, we looked at Python inheritance syntax, inheritance types, Python method overloading, method overriding in Python, and Python super functions.
Tell us in the comment box if something is missing.
