0% found this document useful (0 votes)
2 views20 pages

Inheritance

The document discusses inheritance in Python programming, explaining how a child class can inherit properties and methods from a parent class, promoting code reusability. It covers the syntax for class inheritance, the use of the super() function, and different types of inheritance including multiple, multilevel, and multi-path inheritance. Additionally, it addresses potential issues such as ambiguity in multi-path inheritance and suggests using dynamic algorithms to prevent duplicate member access.

Uploaded by

Vidhya B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views20 pages

Inheritance

The document discusses inheritance in Python programming, explaining how a child class can inherit properties and methods from a parent class, promoting code reusability. It covers the syntax for class inheritance, the use of the super() function, and different types of inheritance including multiple, multilevel, and multi-path inheritance. Additionally, it addresses potential issues such as ambiguity in multi-path inheritance and suggests using dynamic algorithms to prevent duplicate member access.

Uploaded by

Vidhya B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Python Programming – Unit 4 – Inheritance

Dr.VIDHYA B
ASSISTANT PROFESSOR & HEAD
Department of Computer Technology
Sri Ramakrishna College of Arts and Science
Coimbatore - 641 006
Tamil Nadu, India

1
Inheritance
 The process of inheriting the properties of the parent
class into a child class is called inheritance.
 The existing class is called a base class or parent class and the
new class is called a subclass or child class or derived class.
 In object oriented programming, inheritance is an important
aspect.
 The main purpose of inheritance is the reusability of code
because which can use the existing class to create a new class
instead of creating it from scratch.
 In inheritance, the child class acquires all the data members,
properties, and functions from the parent class. Also, a child
class can also provide its specific implementation to the
methods of the parent class.
Inheritance
 In inheritance the base class remains the same.
 Inheritance uses the concept of ‘is-a’ relationship
 Eg:
 Teacher is-a person
 Student is-a person

Here Top-Down approach is


followed where generalized
classes are designed first and then
specialized classes are derived by
inheriting/extending the
generalized classes .
is-a relationship between classes
Inheriting classes in python
Syntax to inherit a class:
Class BaseClass:
Body
Class DerivedClass(BaseClass):
Body
Instead of base class can specify expression like modulename.baseclass
Used when base class is defined in a different module.
Inheriting classes in python

Pass keyword is used when it is not required to add any other properties or
methods to the class
Student class has the same properties and methods of person class
Inheriting classes in python -
Example
Inheriting classes in python -
Example
Explanation:
- Class teacher & student are both
inherited from class person
- Object of derived class is created
as derived classname followed
by empty brackets.
- The __init__() is called
automatically every time the
class is being used to create a
new object
Overriding in python
When a __init__() function is created, the child
class will no longer inherit the parents __init()
To keep the inheritance of the
parent's __init__() function, add a call to the
parent's __init__() function
Example:
Person.__init__(self, fname, lname)
Use the super() Function
Python also has a super() function that
will make the child class inherit all the
methods and properties from its parent.
Using super() function, no need to use
the name of the parent element, it will
automatically inherit the methods and
properties from its parent.
Types of Inheritance
Types of Inheritance
Types of Inheritance
Types of Inheritance
MULTIPLE INHERITANCE
MULTIPLE INHERITANCE

Inside Person class


Name: Jessa Age: 28
Inside Company class
Name: Google location: Atlanta
Inside Employee class
Salary: 12000 Skill: Machine Learning
MULTILEVEL INHERITANCE
MULTILEVEL INHERITANCE
MULTI- PATH INHERITANCE
• Deriving a class from other derived classes that is in turn derived from
same base class is called as multi-path Inheritance.
• The derived class has two immediate base classes –
Derived class1 & Derived class 2, forming a grand parent, parent and
child form of a relationship.
The derived class inherits the feature of base class (grandparent)
Via two separate paths. Hence base class is also known
as the indirect base class.
MULTI- PATH INHERITANCE
Problems in MULTI- PATH
INHERITANCE
Derived class inherits the members of the base class(grandparent)
twice via parent1(derivedclass1) and parent2(derived class2)
This results in ambiguity because a duplicate set of members is created
and this ambiguous situation must be avoided.
Thus a diamond relationship exist when atleast one of the parent
classes can be accessed through multiple paths from the bottom most
class.
To prevent base classes from being accessed more than once dynamic
algorithm can be used called as monotonic

You might also like