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:
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
.
Syntax of Multiple Inheritance :
Class Base1: Body of the class
Class Base2: Body of the class
Class Derived(Base1, Base2): Body of the class
# 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 Inheritanceclass 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 hereclass Cars:# defining constructordef __init__(self, CarName, CarModel):self.name = CarNameself.model = CarModel# defining class methodsdef showName(self):print(self.name)def showModel(self):print(self.Model)# end of class definition# defining another classclass Ids:def __init__(self, CarId):self.CarId = CarIddef getId(self):return self.CarIdclass Main(Cars, Ids): # extends both Cars and Ids classdef __init__(self, name, model, id):Cars.__init__(self, name, model)Ids.__init__(self, id)# Create an object of the subclassMain1 = 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 = 19def getName(self):return self.nameclass Student2:def __init__(self):self.name = 'Ram'self.id = '15'def getName(self):return self.nameclass Students(Student1,Student2):def __init__(self):Student1.__init__(self)Student2.__init__(self)def getName(self):return self.nameStudents1 = Students()print(Students1.getName())
Pros and Cons of Multiple Inheritance :
Pros:
It allows a class to inherit the functionality of more than one base class; thus allowing for modeling of complex relationships.
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.
By having multiple superclasses, your subclass has more opportunities to reuse
the inherited attributes and operations of the superclasses.
Application development time is less and application takes less memory.
Cons:
It can lead to a lot of confusion when two base classes implement a method with the same name.
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:
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.