Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is multiple inheritance in Python?

neverend

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Inheritance : Inheritance allows us to define a class that inherits all the methods and properties from another class. The Parent class is the class being inherited from, also called base class. The Child class is the class that inherits from another class, also called derived class.

Types of Inheritance:

  • Single Inheritance
  • Multi-level Inheritance
  • Multiple Inheritance
  • Multipath Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Multiple Inheritance : Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or the parent class .

svg viewer
Multiple Inheritance Flow Chart

Syntax of Multiple Inheritance :

Class Base1: Body of the class

Class Base2: Body of the class

Class Derived(Base1, Base2): Body of the class

svg viewer
Explanation of the Base Class and Derived Class Flow Chart

Some examples of multiple inheritance

# Multiple Inheritance using two classes:
class Father():
def Driving(self):
print("Father Enjoys Driving")
class Mother():
def Cooking(self):
print("Mother Enjoys Cooking")
class Child(Father, Mother):
def Playing(self):
print("Child Loves Playing")
c = Child()
c.Driving()
c.Cooking()
c.Playing()

Explanation of code:

Here, the father and Mother are the Base classes where we have two print statements and a child class that contains all the methods of the father and mother class.

The child class is also known as Derived class.

We are creating the object for the child class through which we can access the functions of father, mother, and child.

# Creating a multiple inheritance using more than two classes.
class Car():
def Benz(self):
print(" This is a Benz Car ")
class Bike():
def Bmw(self):
print(" This is a BMW Bike ")
class Bus():
def Volvo(self):
print(" This is a Volvo Bus ")
class Truck():
def Eicher(self):
print(" This is a Eicher Truck ")
class Plane():
def Indigo(self):
print(" This is a Indigo plane ")
class Transport(Car,Bike,Bus,Truck,Plane):
def Main(self):
print("This is the Main Class")
B=Transport()
B.Benz()
B.Bmw()
B.Volvo()
B.Eicher()
B.Indigo()
B.Main()

Explanation of Code:

Here we have four classes named, Car,Bike,Bus,Truck,Plane that are called as the base classes. We also have one derived class Transport hat twill be holding all the data of the Car,Bike,Truck,Plane, and Transport.

So, that is why we will be creating the object for the derived class, i.e., the Transport through which we can access all the data of the base class and derived class.

# Performing Addition,Multiplication,Division using Multiple Inheritance
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Summation(1,2))
print(d.Multiplication(1,2))
print(d.Divide(1,2))

Explanation of Code:

Here, in the above program we have three classes. The class with Calculation1 and Calculation2 are the Base classes and the Class named Derived is the combination of the Calculation1 and Calculation2 classes.

Calculation1 class performs the arithmetic operation and Calculation2 performs the Multiplication operation.

Now, in the Derived class we have both an addition and multiplication operation along with its own operation division.

This is how we implement multiple inheritance using Base and derived classes.

# definition of the class starts here
class Cars:
# defining constructor
def __init__(self, CarName, CarModel):
self.name = CarName
self.model = CarModel
# defining class methods
def showName(self):
print(self.name)
def showModel(self):
print(self.Model)
# end of class definition
# defining another class
class Ids:
def __init__(self, CarId):
self.CarId = CarId
def getId(self):
return self.CarId
class Main(Cars, Ids): # extends both Cars and Ids class
def __init__(self, name, model, id):
Cars.__init__(self, name, model)
Ids.__init__(self, id)
# Create an object of the subclass
Main1 = Main('Swift', 500, '1')
Main1.showName()
print(Main1.getId())

Explanation of Code:

The classes Cars and Ids are superclass and Main is the subclass. The class Main extends both Cars and Ids to inherit the properties of both classes. The example is easy to understand if you have some knowledge of Python classes and Python inheritance.

# Example on Multiple Inheritance `
class Student1:
def __init__(self):
self.name = 'Nani'
self.age = 19
def getName(self):
return self.name
class Student2:
def __init__(self):
self.name = 'Ram'
self.id = '15'
def getName(self):
return self.name
class Students(Student1,Student2):
def __init__(self):
Student1.__init__(self)
Student2.__init__(self)
def getName(self):
return self.name
Students1 = Students()
print(Students1.getName())

Pros and Cons of Multiple Inheritance :

Pros:

  1. It allows a class to inherit the functionality of more than one base class; thus allowing for modeling of complex relationships.

  2. You categorize classes in many different ways. Multiple inheritance is a way of showing our natural tendency to organize the world. During analysis, for example, we use multiple inheritance to capture the way users classify objects.

  3. By having multiple superclasses, your subclass has more opportunities to reuse the inherited attributes and operations of the superclasses.

  4. Application development time is less and application takes less memory.

Cons:

  1. It can lead to a lot of confusion when two base classes implement a method with the same name.

  2. The more superclasses your subclass inherits from, the more maintenance you are likely to perform. If one of the superclasses happens to change, the subclass may have to change as well.

Real Time Examples of Multiple Inheritance:

  • Storing a student’s data.
  • Filling taxes and applying deductions.

Conclusion : I Hope you got a clear understanding of what happens under the hood and how you can take the advantage of multiple inheritance in your program and daily life.

RELATED TAGS

python

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring