Python Programming
Language
MICROSOFT AI-102 COURSE
DAY 8
PYTHON LECT # 7
1
Agenda
• Multilevel Inheritance
• Hierarchical Inheritance
• Method Resolution Order
• Access Specifiers: Private, Public, Protected
• Inner/Nested Class
• Association, Aggregation, Composition
2
Multilevel Inheritance
It is a type of inheritance where a class inherits from another class, which
in turn inherits from another class. It forms a hierarchical structure,
similar to a family tree.
Grandparent: The base class with common attributes and methods.
Parent: Inherits from Grandparent and adds its attributes and methods.
Child: The child class inherits attributes and methods from both its parent
and grandparent classes and adds its own attributes and methods.
3
Multilevel Inheritance
super() function:
The super() method in Python is used to access methods from a parent
class in a child class.
It is commonly used to call the constructor of the parent class to ensure
that the parent class is properly initialized when creating an object of the
subclass.
4
Exmp
Link for Notebook:
https://colab.research.google.com/drive/1mIaO5W-SVMKyIVlm-iAioAe7D2txfYeW?usp=sharing
5
Method Resolution Order
MRO is order in which Python searches for inherited methods in a
class hierarchy. When you create a class that inherits from one or
more parent classes, Python uses the MRO to determine which class's
method to call when you invoke a method on an instance of the child
class.
6
Exmp
Link for Notebook: Python 7
https://colab.research.google.com/drive/1mIaO5W-SVMKyIVlm-iAioAe7D2txfYeW?usp=sharing
7
Access Specifiers: Private, Public, Protected
Public: Accessible from anywhere. No special notation is required.
Protected: Accessible within the class and its subclasses. Indicated by
a single underscore _.
Private: Accessible only within the class that defines them. Indicated
by a double underscore __.
8
Exmp
Link for Notebook: Python 7
https://colab.research.google.com/drive/1mIaO5W-SVMKyIVlm-iAioAe7D2txfYeW?usp=sharing
9
Relationship of Classes
Association: A uses-a relationship, where one class uses another class. It is a loose
relationship where neither class owns the other. The objects are independent of each
other.
Aggregation: A has-a relationship with weak ownership, where one class contains a
reference to another class. The contained object can exist independently of the container.
Composition: Composition is a stronger form of "has-a" relationship where one class owns
another class. The contained object cannot exist independently of the container.
Each of these relationships helps to structure code and define the interactions
between different classes in a meaningful way.
10
Exmp
Link for Notebook: Python 7
https://colab.research.google.com/drive/1mIaO5W-SVMKyIVlm-iAioAe7D2txfYeW?usp=sharing
11
Q&A
12