The process of inheriting the properties of the parent class into a child class is called inheritance. The existing class is called a base class or parent class and the new class is called a subclass or child class or derived class.
In this Python lesson, you will learn inheritance, method overloading, method overriding, types of inheritance, and MRO (Method Resolution Order).
In Object-oriented programming, inheritance is an important aspect. The main purpose of inheritance is the reusability of code because we can use the existing class to create a new class instead of creating it from scratch.
In inheritance, the child class acquires all the data members, properties, and functions from the parent class. Also, a child class can also provide its specific implementation to the methods of the parent class.
For example, In the real world, Car is a sub-class of a Vehicle class. We can create a Car by inheriting the properties of a Vehicle such as Wheels, Colors, Fuel tank, engine, and add extra properties in Car as required.
Syntax
class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class
Code language: Python (python)
Also, See
Table of contents
Types Of Inheritance
In Python, based upon the number of child and parent classes involved, there are five types of inheritance. The type of inheritance are listed below:
- Single inheritance
- Multiple Inheritance
- Multilevel inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Now let’s see each in detail with an example.
Single Inheritance
In single inheritance, a child class inherits from a single-parent class. Here is one child class and one parent class.
Example
Let’s create one parent class called ClassOne
and one child class called ClassTwo
to implement single inheritance.
Output
Inside Vehicle class Inside Car class
Multiple Inheritance
In multiple inheritance, one child class can inherit from multiple parent classes. So here is one child class and multiple parent classes.
Example
Output
Inside Person class Name: Jessa Age: 28 Inside Company class Name: Google location: Atlanta Inside Employee class Salary: 12000 Skill: Machine Learning
In the above example, we created two parent classes Person
and Company
respectively. Then we create one child called Employee
which inherit from Person and Company classes.
Multilevel inheritance
In multilevel inheritance, a class inherits from a child class or derived class. Suppose three classes A, B, C. A is the superclass, B is the child class of A, C is the child class of B. In other words, we can say a chain of classes is called multilevel inheritance.
Example
Output
Inside Vehicle class Inside Car class Inside SportsCar class
In the above example, we can see there are three classes named Vehicle
, Car
, SportsCar
. Vehicle
is the superclass, Car
is a child of Vehicle, SportsCar
is a child of Car
. So we can see the chaining of classes.
Hierarchical Inheritance
In Hierarchical inheritance, more than one child class is derived from a single parent class. In other words, we can say one parent class and multiple child classes.
Example
Let’s create ‘Vehicle’ as a parent class and two child class ‘Car’ and ‘Truck’ as a parent class.
Output
This is Vehicle Car name is: BMW This is Vehicle Truck name is: Ford
Hybrid Inheritance
When inheritance is consists of multiple types or a combination of different inheritance is called hybrid inheritance.
Example
Note: In the above example, hierarchical and multiple inheritance exists. Here we created, parent class Vehicle
and two child classes named Car
and Truck
this is hierarchical inheritance.
Another is SportsCar
inherit from two parent classes named Car
and Vehicle
. This is multiple inheritance.
Python super()
function
When a class inherits all properties and behavior from the parent class is called inheritance. In such a case, the inherited class is a subclass and the latter class is the parent class.
In child class, we can refer to parent class by using the super()
function. The super function returns a temporary object of the parent class that allows us to call a parent class method inside a child class method.
Benefits of using the super()
function.
- We are not required to remember or specify the parent
class
name to access its methods. - We can use the
super()
function in both single and multiple inheritances. - The
super()
function support code reusability as there is no need to write the entire function
Example
Output:
Jessa works at Google
In the above example, we create a parent class Company
and child class Employee
. In Employee
class, we call the parent class method by using a super()
function.
issubclass()
In Python, we can verify whether a particular class is a subclass of another class. For this purpose, we can use Python built-in function issubclass()
. This function returns True
if the given class is the subclass of the specified class. Otherwise, it returns False
.
Syntax
issubclass(class, classinfo)
Code language: Python (python)
Where,
class
: class to be checked.classinfo
: aclass
, type, or atuple
of classes or data types.
Example
Also, see Python isinstance().
Method Overriding
In inheritance, all members available in the parent class are by default available in the child class. If the child class does not satisfy with parent class implementation, then the child class is allowed to redefine that method by extending additional functions in the child class. This concept is called method overriding.
When a child class method has the same name, same parameters, and same return type as a method in its superclass, then the method in the child is said to override the method in the parent class.
Example
Output:
max speed is 200 Km/Hour
In the above example, we create two classes named Vehicle
(Parent class) and Car
(Child class). The class Car extends from the class Vehicle so, all properties of the parent class are available in the child class. In addition to that, the child class redefined the method max_speed()
.
Method Resolution Order in Python
In Python, Method Resolution Order(MRO) is the order by which Python looks for a method or attribute. First, the method or attribute is searched within a class, and then it follows the order we specified while inheriting.
This order is also called the Linearization of a class, and a set of rules is called MRO (Method Resolution Order). The MRO plays an essential role in multiple inheritances as a single method may found in multiple parent classes.
In multiple inheritance, the following search order is followed.
- First, it searches in the current parent class if not available, then searches in the parents class specified while inheriting (that is left to right.)
- We can get the MRO of a class. For this purpose, we can use either the
mro
attribute or themro()
method.
Example
class A:
def process(self):
print(" In class A")
class B(A):
def process(self):
print(" In class B")
class C(B, A):
def process(self):
print(" In class C")
# Creating object of C class
C1 = C()
C1.process()
print(C.mro())
# In class C
# [<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]
Code language: Python (python)
In the above example, we create three classes named A
, B
and C
. Class B
is inherited from A
, class C
inherits from B
and A
. When we create an object of the C
class and calling the process()
method, Python looks for the process()
method in the current class in the C
class itself.
Then search for parent classes, namely B
and A
, because C
class inherit from B
and A
. that is, C(B, A)
and always search in left to right manner.
Swaroop says
class A:
def method1(self):
print(“This is method1 from A”)
class B:
def method2(self):
print(“This is method2”)
class C:
def method3(self):
print(“This is method3”)
class D:
def method1(self):
print(“This is method1”)
class E:
def method1(self):
print(“This is method1”)
class F:
def method1(self):
print(“This is method1”)
class G:
def method1(self):
print(“This is method1”)
class H:
def method1(self):
print(“This is method1”)
class I:
def method1(self):
print(“This is method1”)
class J:
def method1(self):
print(“This is method1”)
class K:
def method1(self):
print(“This is method1”)
class L:
def method1(self):
print(“This is method1”)
class M:
def method1(self):
print(“This is method1”)
class N:
def method1(self):
print(“This is method1”)
class O:
def method1(self):
print(“This is method1”)
class P:
def method1(self):
print(“This is method1”)
class Q:
def method1(self):
print(“This is method1”)
class R:
def method1(self):
print(“This is method1”)
class S:
def method1(self):
print(“This is method1”)
class T:
def method1(self):
print(“This is method1”)
class U:
def method1(self):
print(“This is method1”)
class V(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U):
def method3(self):
print(“This is method1 FROM V”)
class W:
def method1(self):
print(“This is method1”)
class X:
def method1(self):
print(“This is method1”)
class Y:
def method1(self):
print(“This is method1”)
class Z:
def method1(self):
print(“This is method1”)
class A1(V,W,X,Y,Z):
def method2(self):
print(“This is method1 from A1”)
obj1 = A1()
obj1.method1()
obj2 = B()
obj2.method2()
Anil Kumlae says
In this code the supercar class is supposed to inherit from truck and car class (not the vehicle class).
class Vehicle:
def vehicle_info(self):
print(“Inside Vehicle class”)
class Car(Vehicle):
def car_info(self):
print(“Inside Car class”)
class Truck(Vehicle):
def truck_info(self):
print(“Inside Truck class”)
# Sports Car can inherits properties of Vehicle and Car
class SportsCar(Car, Vehicle):
def sports_car_info(self):
print(“Inside SportsCar class”)
# create object
s_car = SportsCar()
s_car.vehicle_info()
s_car.car_info()
s_car.sports_car_info()
I think what I am saying is correct in hybrid inheritance
Anil kumlae again says
you can also write —-> sports car(car, truck, vehicle)
Mans says
Hello, master
I am a beginner, but in the Hybrid Class I noticed ” Truck” class is not executed.
I searched for the reason and eventually found out, the argument “Truck” didn’t exist in the def SportCar (Car, Vehicle)
I changed the statement to this, and the “class Truck” was called, as well:
class SportsCar (Car, Truck, Vehicle):
Thank you for your valuable lessons
Mansoor from Iran says
It was a fruitful lesson to me. I tried to learn Python Classes and Inheritance in “w3schools.com”, but it was inadequate and I could not understand the topic wholly.
Tonight, I found PYnative, and you taught the lesson adequately and clearly. Now, I know what Python inheritance is! I only need some exercises based on your examples to memorize all the cases and their orders.
I also will try to learn other Python instructions in Pynative.
Thank you so much 🙂
Satyam shukla says
Thank you, Vishal(Founder of PYnative.com ) for creating this article.
it really helpful for me to learn python.
PRAKASHAN says
EACH TOPIC BEATIFULLY EXPLAINED…YOU ARE DOING A GREAT SERVICE…CONTINUE
Rajat Bhardwaj says
It Gives Us Error::
TypeError: Cannot create a consistent method resolution
order (MRO) for bases A, B
Commander says
use this code, the author did a small mistake by inheriting class A in B, just remove that A. It should look like classB()
Adil says
u have to change the order of multiple inheritance in class c (B,A), that it