INHERITANCE IN PYTHON
TH.S [Link]
OUTLINE
Introduction Inheritance Practice
1 2 3
Type and Attribute of Inheritance
I. INTRODUCTION ABOUT INHERITANCE
Inheritance is a way to define a new class (child
class or derived class) based on an existing
class (parent class or base class).
The child class inherits attributes and methods
from the parent class, allowing you to reuse
code and extend functionality.
I. INTRODUCTION ABOUT INHERITANCE
II. BASIC INHERITANCE
Step 1: Create a base class : Example called Shape that represents geometric shapes.
II. BASIC INHERITANCE
Step 2: Creating Sub-Classes
Create specific shape classes that will inherit from the Shape class. We will create classes for Circle and Rectangle shapes.
II. BASIC INHERITANCE
Step 3: Using the Sub-Classes
How to use the sub-classes we’ve created. Create a new file called [Link] and use the Circle and
Rectangle classes:
III. TYPE INHERITANCE
❑ Single inheritance
❑ Multilevel inheritance
❑ Multiple inheritance
❑ Hierarchical inheritance
❑ Hybrid inheritance
III.1 SINGLE INHERITANCE
Child class inherits the properties (i.e. variables)
and behavior (i.e. methods) from only a single
parent class,
It is called single inheritance in Python.
III.1 SINGLE INHERITANCE
❑ Example
III.2 MULTILEVEL INHERITANCE
The child class derives from a class which
already derived from another class. .
III. 2 MULTILEVEL INHERITANCE
❑ Example
III.3 MULTIPLE INHERITANCE:
❑ Can be used to create classes with a combination of features from different domains,
such as:
✓ Creating a class that combines functionalities from different libraries or frameworks
✓ Building complex hierarchies by inheriting from multiple related classes
✓ Implementing mixins to add specific behavior to a class without duplicating code.
III.3 MULTIPLE INHERITANCE:
❑ Example
III.3 MULTIPLE INHERITANCE:
❑ Example
III.4 HIERARCHICAL INHERITANCE:
❑ When more than one child class is derived from a
single parent class, this type of inheritance is called
hierarchical inheritance in Python.
❑ It involves multiple classes inheriting from a single
parent class or the same class. In the above figure,
multiple classes, such as class B, class C, and class D
is derived from a single parent class A.
III.4 HIERARCHICAL INHERITANCE:
Example
III.5 HYBRID INHERITANCE
Hybrid inheritance in Python is a combination of hierarchical inheritance and multiple
inheritance within a single program or class hierarchy.
It allows us to create complex class structures that can serve various purposes.
III.5 HYBRID INHERITANCE
Example
IV. POLYMORPHISM
IV.1 POLYMORPHISM TYPES
IV.2 POLYMORPHISM - RUNTIME OR METHOD OVERRIDING
IV.2.1 METHOD OVERRIDING IN PYTHON
Method overriding is an ability of any object-oriented
programming language that allows a subclass or child class to
provide a specific implementation of a method that is already
provided by one of its super-classes or parent classes.
When a method in a subclass has the same name, the same
parameters or signature, and same return type(or sub-type) as a
method in its super-class, then the method in the subclass is
said to override the method in the super-class.
IV.2.1 METHOD OVERRIDING IN PYTHON
Example
IV.2.2 METHOD OVERRIDING MULTIPLE INHERITANCE
Example
IV.2.2 METHOD OVERRIDING MULTIPLE INHERITANCE
Example
IV.2.3 METHOD OVERRIDING MULTILEVEL INHERITANCE
Example
IV.2.4 CALLING THE PARENT’S METHOD WITHIN THE OVERRIDDEN
METHOD
❑ Parent class methods can also be called within the overridden
methods. This can generally be achieved by two ways.
➢ Using Classname: Parent’s class methods can be called by using the
Parent [Link] inside the overridden method.
IV.2.4 CALLING THE PARENT’S METHOD WITHIN THE OVERRIDDEN
METHOD
❑ Using Super()
✓ Python super() function provides us the facility to refer to the parent class
explicitly.
✓ It is basically useful where we have to call superclass functions. It returns
the proxy object that allows us to refer parent class by ‘super’.
IV.2.4 CALLING THE PARENT’S METHOD WITHIN THE OVERRIDDEN
METHOD
❑ Example
IV.2.4 CALLING THE PARENT’S METHOD WITHIN THE OVERRIDDEN
METHOD
❑ Example
V. EXAMPLE INHERITANCE
❑ Bài số 1:
✓ Học sinh viết code theo cây sau.
V. PRACTICE
Bài số 2:
❑ Chương trình quản lý gia phả.
➢ Cấu trúc này thể hiện :Grandparent (Ông/Bà)
→ Parent (Cha/Mẹ) → Child (Con cái), .
➢ Các chức năng
✓ Thêm thành viên vào cây gia phả.
✓ Hiển thị thông tin chi tiết của từng thành
viên.
✓ Xác định quan hệ giữa các thành viên.