Introduction to Multiple Inheritance in Python
Multiple Inheritance in python is a well-known feature that is supported by all the major object-oriented programming languages. It can be described as a process where the child class or object inherits the methods and attributes from one or more parent classes. It is a hierarchical process that leads to reusability of a code, higher performance and flexibility. It also has its own disadvantages like increased complexity, more chances of ambiguity, and deeper coding knowledge.
The syntax of multiple inheritances involving two base classes and a derived class is as shown below.
class Base_1: pass class Base_2: pass class DerivedClass(Base_1, Base_2): pass
The syntax of multiple inheritances involving three base classes and a derived class is as shown below.
class Base_1: pass class Base_2: pass class Base_3: pass class DerivedClass(Base_1, Base_2, Base_3) pass
Examples of Multiple Inheritance in Python
Let’s go through the following program codes in Python, which implements the concept of multiple inheritances.
We will begin with a simple example so as to understand the working of the concept.
class A: def A(self): print('This is class A.') class B: def B(self): print('This is class B.') class C(A,B): def C(self): print('This is class C which inherits features of both classes A and B.') o = C() o.A() o.B() o.C()
Going through the python programming code, as shown above, there are three classes viz. class A, class B, and Class C. Class C is a derived class that inherits features from classes A and B. class A and class B thus act as base classes. Finally, we have a variable o that we have assigned to the derived class C. the variable operates all the classes. The program code doesn’t perform any complex task; however, it allows us to familiarize ourselves with the concept.
Each of the three classes just has print statements, and when the program code is executed, the statements are printed.
We’ll extend this example to a more logical form. Let’s consider that we want to find the area of a rectangle. In this case, two inputs are required, viz. length and breadth of the rectangle. Using the concept of multiple inheritances, the area of the rectangle can be calculated. Here, three classes are required, of which two would act as base classes, and one would be the derived class. Two base classes for length and breadth, respectively and the derived class would be used for the calculation of the area of the rectangle. This class will derive length and breadth inputs from the respective base classes.
The program code implementing the above-discussed concept is as follows. Go through the code so as to understand each of its components properly.
class length: l = 0 def length(self): return self.l class breadth: b = 0 def breadth(self): return self.b class rect_area(length, breadth): def r_area(self): print("The area of rectangle with length "+str(self.l)+" units and breadth "+ str(self.b)+" units is "+str(self.l * self.b)+" sq. units.") o = rect_area() o.l = int(input("Enter the required length for rectangle: ")) o.b = int(input("Enter the required breadth for rectangle: ")) o.r_area()
Let’s go through the program code step-by-step. First, we have a class length. In this class, there’s a variable l. Initially, we have set the value of this variable to zero. Then we have a routine length(), which returns the value of variable l. Similarly, we have another class breadth which has a variable b, initially assigned the value of zero. Routine breadth() returns the value of variable b. Finally, we have a third class which is rect_area, that derives value from both the base classes. It has a routine r_area() that gives the rectangle area based on the values of length and breadth that comes from the respective two base classes.
In the end, we create a variable o which we assign to class rect_area. This variable acts as an operator, which we further operate upon variables from base classes. So, with the derived class operating upon the variables of base classes, the assignment happens. We can see that variables l and b are assigned values through input boxes. By default, they are of string type, so we converted them to numbers using the int() function. The o.r_area() function gives the area.
We checked the above program through a series of inputs. When the program is executed, the user is asked to provide the input for variables l and b, as shown below.
- First, the user is asked to input the length for the rectangle in the text.
- When a requisite value for length is passed, the breadth value needs to be passed.
- As we can see, we passed two integer values and got the output in a well-formatted form.
The above program implemented works well only with integer values. If the user tries to pass decimal values, he may end up having an error. In order to overcome this, we need to convert the string input values to float (or any decimal type).
The program code will remain the same, except for the function used for type conversion. The program code is as shown below.
class length: l = 0 def length(self): return self.l class breadth: b = 0 def breadth(self): return self.b class rect_area(length, breadth): def r_area(self): print("The area of rectangle with length "+str(self.l)+" units and breadth "+ str(self.b)+" units is "+str(self.l * self.b)+" sq. units.") o = rect_area() o.l = float(input("Enter the required length for rectangle: ")) o.b = float(input("Enter the required breadth for rectangle: ")) o.r_area()
Amongst various types of inheritances, multiple inheritances is a type that python supports. Python offers an easy-to-implement methodology in this context. The concept is quite useful in situations involving numerous interrelated variables and wherein the relations need to be regulated properly.
This is a guide to Multiple Inheritance in Python. Here we discuss the Introduction and examples of multiple inheritances in python along with code implementation. You may also look at the following articles to learn more –