0% found this document useful (0 votes)
4 views17 pages

Session 6

This presentation introduces Object-Oriented Programming (OOP) in Python, highlighting its key concepts such as classes, objects, and inheritance. It emphasizes the benefits of OOP, including modularity, reusability, and maintainability, while explaining the principles of polymorphism and duck typing. The document outlines different types of inheritance and provides examples to illustrate these concepts.

Uploaded by

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

Session 6

This presentation introduces Object-Oriented Programming (OOP) in Python, highlighting its key concepts such as classes, objects, and inheritance. It emphasizes the benefits of OOP, including modularity, reusability, and maintainability, while explaining the principles of polymorphism and duck typing. The document outlines different types of inheritance and provides examples to illustrate these concepts.

Uploaded by

Mohamed Shaarawy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like