Python Multiple Inheritance

As discussed in python inheritance, inheritance is a process to inherit all the properties and methods from one class (base) to another class (child).

 

If required, you can implement two types of inheritances in python; those are

 

  • Multiple Inheritance
  • Multilevel Inheritance

Multiple Inheritance in Python

Suppose, if you create a class that inherits from more than one class, we will call it multiple inheritance in python.

 

To implement the multiple inheritance in python, you need to create a class that inherits from various classes, as shown below.

 

class X:
   #Class Statements
class Y:
   #Class Statements
class Z(X, Y):
   #Child/Derived Class Statements

If you observe the above multiple inheritance example, the class Z is derived from both X and Y classes. Now, class Z will have access to all the resources of both X and Y classes.

Python Multiple Inheritance Example

Following is the example of multiple inheritance in python.

 

class Name:
   def getname(self, name):
     print("Name: {}".format(name))

class Location:
   def getlocation(self, location):
     print("Location: {}".format(location))

class User(Name, Location):
   pass

u1 = User()
u1.getname("Suresh Dasari")
u1.getlocation("Hyderabad")

The above python multiple inheritance example will return the result as shown below.

 

Name: Suresh Dasari
Location: Hyderabad

Multilevel Inheritance in Python

In python, you also create a class that inherits all the features from the derived class, and we will call it a multilevel inheritance.

 

As discussed, inheritance is a process to inherit all the resources from base class to derived class, and multiple inheritance is a process to create a derived class that inherits resources from more than one base class. Similarly, if you create a class that inherits resources from a derived class, we will call it a multilevel inheritance.

 

To implement the multilevel inheritance in python, you need to create a class that inherits from the derived class, as shown below.

 

class X:
   #Class Statements

class Y(X):
   #Dervied Class Statements

class Z(Y):
   #Child/Derived Class Statements

If you observe the above multilevel inheritance example, the class Y is derived from the base X class, and the Z class is created to inherit all the resources from the derived Y class. Now, the Z class will have access to all the resources of both X and Y classes.

Python Multilevel Inheritance Example

Following is the example of multilevel inheritance in python.

 

class Name:
   def getname(self, name):
     print("Name: {}".format(name))

class Location(Name):
   def getlocation(self, location):
     print("Location: {}".format(location))

class User(Location):
   pass

u1 = User()
u1.getname("Suresh Dasari")
u1.getlocation("Hyderabad")

The above python multilevel inheritance example will return the result as shown below.

 

Name: Suresh Dasari
Location: Hyderabad

 This is how you can implement multiple and multilevel inheritance in python based on your requirements.