Introduction To Programming 2
Python
Lecture 7
Department of Computer Engineering
Astana IT University
Zamart Ramazanova
Senior-Lecturer
[Link]@[Link]
January 2025
Topics
• Inheritance
• Association
• Polymorphism
2
Introduction
• Classes allow you to modify a program
without really making changes to it.
• To elaborate, by sub classing a class,
you can change the behavior of the
program by simply adding new
components to it rather than rewriting
the existing components.
3
Copyright © 2018 Pearson Education, Inc.
Introduction
• As we’ve seen, an instance (object) of a class
inherits the attributes of that class.
• However, classes can also inherit attributes from
other classes.
• Hence, a subclass inherits from a superclass
allowing you to make a generic superclass that
is specialized subclasses.
• The subclasses can override the logic in a
superclass, allowing you to change the behavior
of your classes without changing the superclass
at all.
4
Copyright © 2018 Pearson Education, Inc.
Inheritance
• In OOP, Inheritance is an object or
class is based on another object or
class, using the same implementation,
it is a mechanism for code reuse.
• The relationships of objects or classes
though inheritance give rise to a
hierarchy.
• Inheritance was invented in 1967 for
SIMULA (programming language) .
5
Copyright © 2018 Pearson Education, Inc.
Inheritance
• In OOP, Inheritance is an object or
class is based on another object or
class, using the same implementation,
it is a mechanism for code reuse.
• The relationships of objects or classes
though inheritance give rise to a
hierarchy.
• Inheritance was invented in 1967 for
SIMULA (programming language) .
6
Copyright © 2018 Pearson Education, Inc.
Inheritance
• Inheritance is the capability of one class to
derive or inherit the properties from
another class.
• The class that derives properties is called
the derived class or child class and the
class from which the properties are being
derived is called the base class or parent
class.
7
Copyright © 2018 Pearson Education, Inc.
The benefits of inheritance
• It represents real-world relationships well.
• It provides the 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.
• 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.
8
Copyright © 2018 Pearson Education, Inc.
Example of Inheritance
9
Copyright © 2018 Pearson Education, Inc.
Inheritance
• In some languages, Inheritance
implements IS-A Relationship.
• There are various types of inheritance,
depending on paradigm and specific
language.
10
Copyright © 2018 Pearson Education, Inc.
Types of Inheritance
11
Copyright © 2018 Pearson Education, Inc.
Single Inheritance
Single-level inheritance enables a
derived class to inherit characteristics
from a single-parent class.
12
Copyright © 2018 Pearson Education, Inc.
Multilevel Inheritance
Multi-level inheritance enables a derived class
to inherit properties from an immediate parent
class which in turn inherits properties from his
parent class.
13
Copyright © 2018 Pearson Education, Inc.
Multilevel Inheritance
14
Copyright © 2018 Pearson Education, Inc.
Hierarchical Inheritance
Hierarchical-level inheritance enables more
than one derived class to inherit properties
from a parent class.
15
Copyright © 2018 Pearson Education, Inc.
Multiple Inheritance
Multiple-level inheritance enables one derived
class to inherit properties from more than one
base class.
16
Copyright © 2018 Pearson Education, Inc.
17
18
19
20
21
Let’s Take a Break
22
Copyright © 2018 Pearson Education, Inc.
Advantages of Inheritance
• It permits code to be reused and avoids
code duplication.
• It permits for the creation of hierarchical
classifications.
• It permits for extensibility, as new
subclasses can be made.
• It permits for less demanding support
and debugging.
23
Copyright © 2018 Pearson Education, Inc.
Disadvantages of Inheritance
• Tight coupling: Inheritance makes a firmly
coupled relationship among classes, making it
troublesome to adjust existing code without
breaking other program parts.
• Increased complexity: Inheritance can increment
the complexity of a program, as increasing
classes are included to back the inheritance
hierarchy.
• Overuse: Inheritance ought to be utilized
sparingly because it can lead to overcomplicated
designs and implementations that are
troublesome to keep up with.
Copyright © 2018 Pearson Education, Inc.
24
Association
• In OOP, association defines a relationship
between classes of objects that allows one
object instance to cause another to perform
an action on its behalf.
• Association implements USES-A
Relationship
• This relationship is structural, because it
specifies that objects of one kind are
connected to objects of another and does
not represent behavior.
25
Copyright © 2018 Pearson Education, Inc.
Association
26
Copyright © 2018 Pearson Education, Inc.
Association
27
Copyright © 2018 Pearson Education, Inc.
Polymorphism
• Polymorphism is another fundamental concept
in OOP, which means multiple forms.
• Polymorphism allows us to use a single
interface with different underlying forms such as
data types or classes.
• You all must have used GPS for navigating the
route, Isn’t it amazing how many different routes
you come across for the same destination
depending on the traffic, from a programming
point of view this is called ‘polymorphism’.
28
Copyright © 2018 Pearson Education, Inc.
Polymorphism
29
Copyright © 2018 Pearson Education, Inc.
Advantages of Polymorphism
• It permits for the execution of dynamic
dispatch and the implementation of
interfaces.
• It reduces the number of lines of code and
makes it simpler to maintain.
• It permits for the execution of more
generic algorithms.
• It permits the execution of more adaptable
programs.
30
Copyright © 2018 Pearson Education, Inc.
Disadvantages of Polymorphism
• Trouble investigating: Polymorphism can make it
challenging to investigate code because it can
be hard to track the stream of execution when
the same code is executing in several ways.
• Execution issues: Polymorphism can lead to
execution issues, as the framework must check
each object's sort to decide which strategy to
execute.
• Superfluous complexity: Polymorphism can too
lead to pointless complexity in the event that it is
utilized when a less complex approach would
suffice.
Copyright © 2018 Pearson Education, Inc.
31
Polymorphism in Python Examples
32
Copyright © 2018 Pearson Education, Inc.
Types of Polymorphism in Python
33
Copyright © 2018 Pearson Education, Inc.
• Compile-time Polymorphism:
A compile- time Polymorphism also
called as static polymorphism which gets
resolved during the compilation time of
the program. One common example is
“method overloading”.
34
Copyright © 2018 Pearson Education, Inc.
35
Copyright © 2018 Pearson Education, Inc.
• Run-time Polymorphism:
A run- time Polymorphism is also, called
as static polymorphism where it gets
resolved into the run time of the
program. One common example of Run-
time polymorphism is “method
overriding”.
36
Copyright © 2018 Pearson Education, Inc.
37
Copyright © 2018 Pearson Education, Inc.
Parametric Polymorphism
(Generics):
• Python supports parametric polymorphism
through the use of generics using type
hints and annotations. You can use type
variables to indicate that a function or
class can work with various data types.
• Python provides flexibility in achieving
different types of polymorphism, making it
a versatile language for object-oriented
programming.
38
Copyright © 2018 Pearson Education, Inc.
Parametric Polymorphism
(Generics):
39
Copyright © 2018 Pearson Education, Inc.
Difference between
Inheritance and Polymorphism
40
Copyright © 2018 Pearson Education, Inc.
Limitations of Inheritance and
Polymorphism
Inheritance and polymorphism are useful tools for software development and
object-oriented programming. However, there are several limitations to be
aware of when using inheritance and polymorphism.
1. Unnecessary complexity: Inheritance and polymorphism can add
unnecessary complexity to software design if not used carefully. This can
result in software that is difficult to maintain and debug.
2. Decreased performance: Inheritance and polymorphism can reduce
software performance due to the additional overhead associated with their
use.
3. Fragile base class problem: The "Fragile Base Class Problem" occurs
when changes to a base class cause unexpected behavior in child classes.
4. Limited reusability: Inheritance and polymorphism can limit code
reusability, as it is often difficult to reuse code based on inheritance or
polymorphism.
5. Lack of flexibility: Inheritance and polymorphism can lack the flexibility
necessary for some software designs. For example, if a software design
requires the ability to extend the behavior of an object dynamically, then
there may be better approaches than inheritance and polymorphism. 41
Copyright © 2018 Pearson Education, Inc.
Summary
• This chapter covered:
• Inheritance
- The benefits of inheritance
- Types of Inheritance
- Advantages and disadvantages of Inheritance
• Association
• Polymorphism
- Advantages and disadvantages of Polymorphism
- Differences between Inheritance and Polymorphism
42
Ramazanova Zamart. Introduction to Programming 2