PPS U5 Module3 (A)
PPS U5 Module3 (A)
In python, a derived class can inherit base class by just mentioning the base in the
bracket after the derived class name.
Syntax :
class Show(Addition):
Types of Inheritance in Python :
Types of Inheritance depends upon the number of child and parent classes
involved.
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance:
1. Single inheritance: When a child class inherits from only one parent class, it is
called single inheritance.
# Example of inheritance
class Parent():
def first(self):
print('first
function')
#child class show inherits the base class Addition ('Sum=', 30)
class Show(Addition):
def display(self):
print("This is Addition")
2. Multi-Level inheritance
When a child class becomes a parent class for another child class.
# Example of Multilevel inheritance
class Parent:
def fun1(self):
print("This is function 1")
class Child(Parent):
def fun2(self):
print("This is function 2")
class Child2(Child):
def fun3(self): Output :
print("This is function 3") This is function 1
obj = Child2() This is function 2
obj.fun1() This is function 3
obj.fun2()
obj.fun3()
3. Multiple Inheritance
Python provides us the flexibility to inherit multiple base classes in the child
class.
When a child class inherits from multiple parent classes, it is called multiple
inheritance.
# Example of Multiple inheritance
class Parent1:
def fun1(self):
print("This is function 1")
class Parent2:
def fun2(self):
print("This is function 2")
class Child(Parent1 ,
Parent2): def fun3(self):
print("This is function 3")
obj = Child()
obj.fun1()
obj.fun2()
obj.fun3()
class Addition: # Example of Multiple inheritance class Child(Addition , Sub1):
def add(self): def fun3(self):
a=10 print("This is Program")
b=20
sum=a+b obj = Child()
print('sum=',sum) obj.add()
class Sub1: obj.sub()
def sub(self): obj.fun3()
a=50 Output:
b=20 ('sum=', 30)