0% found this document useful (0 votes)
16 views

PPS U5 Module3 (A)

1. Inheritance allows a child class to inherit attributes and behaviors from a parent class, providing code reusability. 2. In Python, a child class inherits from a parent class by specifying the parent class in parentheses after the child class name. 3. There are different types of inheritance in Python including single inheritance, multilevel inheritance, multiple inheritance, and hierarchical inheritance, depending on the number of parent and child classes involved.

Uploaded by

Sanny Wagh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

PPS U5 Module3 (A)

1. Inheritance allows a child class to inherit attributes and behaviors from a parent class, providing code reusability. 2. In Python, a child class inherits from a parent class by specifying the parent class in parentheses after the child class name. 3. There are different types of inheritance in Python including single inheritance, multilevel inheritance, multiple inheritance, and hierarchical inheritance, depending on the number of parent and child classes involved.

Uploaded by

Sanny Wagh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

The benefits of inheritance are:

1. It represents real-world relationships well.


2. It provides reusability of a code. We don’t have to write the same code again and
again. Also, it allows us to add more features to a class without modifying it.
3. It is transitive in nature, which means that if class B inherits from another class A,
then all the subclasses of B would automatically inherit from class 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')

# This is child class


class Child(Parent): Output :
def second(self):
print('second first function
function') second function
obj = Child()
obj.first()
obj.second()
class Addition:
def add(self): obj = Show()
obj.display()
a=10
obj.add()
b=20
sum=a+b
print("Sum=",sum) Output :
This is Addition

#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)

sub=a-b ('sub=', 30)

print('sub=',sub) This is Program


Hierarchical Inheritance
Hierarchical inheritance involves multiple inheritance from the same base or
parent class.
Thanks

You might also like