Introduction to Object-
Oriented Programming (OOP)
In this presentation, we'll explore the world of Object-Oriented Programming (OOP) in
Python. OOP is a powerful paradigm for structuring programs by creating objects that
encapsulate data (attributes) and functionality (methods). It allows us to model real-
world entities and their interactions, making code more modular, reusable, and
maintainable.
OOP is based on three
fundamental concepts:
Classes:
Blueprints that define the properties and behavior of objects.
Objects: Instances of classes that encapsulate data (attributes) and functionality
(methods). Inheritance: A mechanism for creating new classes (subclasses) that
inherit properties and behavior from existing classes (superclasses).
WHAT ARE
CLASSES AND
OBJECTS?
Class: A blueprint for creating objects
Object: An instance of a class
Example: Car is a class, while Toyota
and Honda are objects
Creating a Class in Python
Explanation:
__init__: Constructor method to initialize objects
self: Represents the instance of the class
display_info(): Method to print object details 108
CREATING AND
USING OBJECTS
INSTANTIATING OBJECTS FROM A CLASS
Each object has unique attributes
Class Attributes vs Instance Attributes
110
INHERITANCE
WHAT IS
INHERITANCE?
DEFINITION: A CLASS (CHILD) CAN
INHERIT PROPERTIES AND METHODS
FROM ANOTHER CLASS (PARENT)
WHY USE INHERITANCE?
Code reusability
Reduces redundancy
Supports hierarchical relationships
113
Types of Inheritance
1. Single Inheritance – One child inherits from one parent
2. Multiple Inheritance – Child class inherits from multiple parent classes
3. Multilevel Inheritance – A child class becomes a parent for another child
4. Hierarchical Inheritance – Multiple child classes inherit from a single parent
A way to make your code easier
WHAT IS
POLYMORPHISM?
WHY USE POLYMORPHISM?
Definition: Different classes
can have methods with the
same name but different
implementations
Improves code flexibility
Allows different object types to be
used interchangeably
POLYMORPHISM EXAMPLE
METHOD OVERRIDING VS
METHOD OVERLOADING
DUCK TYPING
IN PYTHON
"If it looks like a duck and quacks like a
duck, it must be a duck."
Python allows dynamic method calls
based on object behavior