SlideShare a Scribd company logo
Object-Oriented
Programming
(OOP) in python
BAKHAT ALI
Institute of Geoinformatics and Earth Observation,
Pir Mehr Ali Shah Arid Agriculture University Rawalpindi , Punjab, Pakistan
bakhtali21uaar@gmail.com
Object-Oriented Programming
(OOP)
Object-Oriented Programming (OOP) is a programming paradigm that uses
"objects" to encapsulate data and functionality, promoting organized code
and efficient data reuse.
Key Features of OOP
Encapsulation: Protects and bundles data and methods together, ensuring outside interference is
limited.
Abstraction: Simplifies complex systems by exposing only essential features while hiding
unnecessary details.
Inheritance: Allows one class to inherit attributes and behaviors from another, enabling code reuse
and creating a class hierarchy.
Polymorphism: Grants objects the ability to be treated as instances of their parent class, while
allowing their specific implementations to define how actions are executed.
Classes and Objects
Classes
A class is a blueprint to create objects, comprising attributes (data) and
methods (functions).
Objects
An object is an instantiation of a class, representing a specific instance
with defined attributes.
Class and Object Syntax
class ClassName: # Define a class def
__init__(self, param): # Constructor
self.attribute = param # Instance variable
def method_name(self): # Method definition
return self.attribute # Creating an object
(instantiation) object_instance =
ClassName(value)
Example of Class and Object
class Car: def __init__(self, make, model,
year): self.make = make self.model = model
self.year = year def display_info(self):
return f"{self.year} {self.make}
{self.model}" # Instantiating the object
my_car = Car("Toyota", "Corolla", 2021)
print(my_car.display_info()) # Output: 2021
Toyota Corolla
. Inheritance
Inheritance allows one class (child class) to
inherit the properties and methods of
another (parent class).
Types of Inheritance
SINGLE INHERITANCE: A CHILD CLASS INHERITS FROM ONE PARENT
CLASS.
class Animal: def sound(self): return "Some
sound" class Dog(Animal): def sound(self):
return "Bark" dog = Dog()
print(dog.sound()) # Output: Bark
Multilevel Inheritance:
A class inherits from a
derived class.
CLASS PUPPY(DOG): DEF SOUND(SELF): RETURN "YIP"
PUPPY = PUPPY() PRINT(PUPPY.SOUND()) # OUTPUT: YIP
Hierarchical Inheritance: Multiple classes inherit from
one parent class.
class Vehicle: def type(self): return
"Vehicle" class Car(Vehicle): def
type(self): return "Car" class
Bike(Vehicle): def type(self): return
"Bike" car = Car() bike = Bike()
print(car.type()) # Output: Car
print(bike.type()) # Output: Bike
Multiple Inheritance: A class can inherit from multiple parent
classes.
class A: def method_a(self): return "Method
A" class B: def method_b(self): return
"Method B" class C(A, B): def
method_c(self): return "Method C" obj = C()
print(obj.method_a()) # Output: Method A
print(obj.method_b()) # Output: Method B
Polymorphism
POLYMORPHISM ALLOWS METHODS TO DO DIFFERENT THINGS BASED ON THE OBJECT TYPE OR CLASS
INVOKING THEM.
Compile-time Polymorphism (Static Binding):
Achieved mainly through method overloading.
lass Math: def add(self, a, b): return a +
b def add_with_three(self, a, b, c): return
a + b + c math = Math() print(math.add(5,
10)) # Output: 15
print(math.add_with_three(5, 10, 15)) #
Output: 30
Run-time Polymorphism (Dynamic Binding):
Achieved through method overriding.
lass Shape: def area(self): pass class
Rectangle(Shape): def __init__(self,
length, width): self.length = length
self.width = width def area(self): return
self.length * self.width class
Circle(Shape): def __init__(self, radius):
self.radius = radius def area(self): return
3.14 * (self.radius**2) shapes =
[Rectangle(10, 5), Circle(7)] for shape in
shapes: print(shape.area()) # Output: 50,
153.86
Encapsulation
ENCAPSULATION COMBINES ATTRIBUTES AND METHODS IN A CLASS AND RESTRICTS ACCESS USING
ACCESS MODIFIERS.
class BankAccount: def
__init__(self, balance):
self.__balance = balance #
Private attribute def deposit(self,
amount): self.__balance +=
amount def withdraw(self,
amount): if amount <=
self.__balance: self.__balance -=
amount else: print("Insufficient
funds.") def get_balance(self):
return self.__balance account =
BankAccount(1000)
account.deposit(500)
print(account.get_balance()) #
Output: 1500
account.withdraw(200)
print(account.get_balance()) #
Output: 1300
Abstraction
ABSTRACTION SIMPLIFIES COMPLEX SYSTEMS BY HIDING UNNECESSARY DETAILS AND EXPOSING ONLY THE
NECESSARY PARTS USING ABSTRACT CLASSES.
from abc import ABC, abstractmethod class Device(ABC):
@abstractmethod def turn_on(self): pass class
Television(Device): def turn_on(self): return "TV is now ON."
class Radio(Device): def turn_on(self): return "Radio is now
ON." tv = Television() radio = Radio() print(tv.turn_on()) #
Output: TV is now ON. print(radio.turn_on()) # Output: Radio
is now ON.
Comprehensive OOP Features Table with Code Examples
Feature Definition Syntax Example Code Example
Class A blueprint for objects. class ClassName: class Vehicle: pass
Object An instance of a class. object_name = ClassName() my_car = Vehicle()
Attributes Data stored in an object. self.attribute_name = value self.make = "Toyota"
Methods Functions defined inside a class. def method_name(self): def display_info(self): return self.make
Constructor
A method called when an object is
created.
def __init__(self, parameters):
def __init__(self, make, model):
self.make = make
Encapsulation Restricting access to object data.
Use of
underscores: self.__private_data
self.__balance = 1000
Inheritance Deriving new classes from existing ones. class DerivedClass(ParentClass): class Dog(Animal):
Polymorphism
Methods behaving differently based on
the object.
Method Overriding
class Cat(Animal): def sound(self):
return "Meow"
Abstraction Hiding complex implementation details. Use of abstract methods from abc import ABC, abstractmethod
OOP
Summary:
Encapsulation: Protects and bundles
data/functionality.
Abstraction: Simplifies interfaces.
Inheritance: Supports code reuse and
hierarchy.
Polymorphism: Flexible interfaces for
objects.

More Related Content

PPTX
OOPS 46 slide Python concepts .pptx
PPTX
Inheritance
PPTX
Introduction to OOP_Python_Lecture1.pptx
 
PPTX
OOP Concepts Python with code refrences.pptx
PPTX
Class and Objects in python programming.pptx
PPTX
Python advance
PDF
Object-Oriented Programming System presentation
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
OOPS 46 slide Python concepts .pptx
Inheritance
Introduction to OOP_Python_Lecture1.pptx
 
OOP Concepts Python with code refrences.pptx
Class and Objects in python programming.pptx
Python advance
Object-Oriented Programming System presentation
Unit 3-Classes ,Objects and Inheritance.pdf

Similar to Object-Oriented Programming (OO) in pythonP) in python.pptx (20)

PPTX
OOPS.pptx
PPTX
object oriented programming(PYTHON)
PPTX
arthimetic operator,classes,objects,instant
PPTX
Python programming computer science and engineering
 
PPTX
9-_Object_Oriented_Programming_Using_Python 1.pptx
PPTX
Presentation_4516_Content_Document_20250204010703PM.pptx
PDF
Lecture # 02 - OOP with Python Language by Muhammad Haroon
PDF
Object Oriented programming Using Python.pdf
PDF
9-_Object_Oriented_Programming_Using_Python.pdf
PDF
Object oriented Programning Lanuagues in text format.
PPTX
Problem solving with python programming OOP's Concept
PPTX
OOP concepts -in-Python programming language
PDF
OOP in Python, a beginners guide..........
PPTX
Basics of Object Oriented Programming in Python
PPTX
Unit – V Object Oriented Programming in Python.pptx
PPTX
Python – Object Oriented Programming
PPTX
Python programming Concepts (Functions, classes and Oops concept
PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
PPTX
Object Oriented Programming.pptx
OOPS.pptx
object oriented programming(PYTHON)
arthimetic operator,classes,objects,instant
Python programming computer science and engineering
 
9-_Object_Oriented_Programming_Using_Python 1.pptx
Presentation_4516_Content_Document_20250204010703PM.pptx
Lecture # 02 - OOP with Python Language by Muhammad Haroon
Object Oriented programming Using Python.pdf
9-_Object_Oriented_Programming_Using_Python.pdf
Object oriented Programning Lanuagues in text format.
Problem solving with python programming OOP's Concept
OOP concepts -in-Python programming language
OOP in Python, a beginners guide..........
Basics of Object Oriented Programming in Python
Unit – V Object Oriented Programming in Python.pptx
Python – Object Oriented Programming
Python programming Concepts (Functions, classes and Oops concept
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
Object Oriented Programming.pptx
Ad

More from institute of Geoinformatics and Earth Observation at PMAS ARID Agriculture University of Rawalpindi (16)

PPTX
đŸŒŸ Introduction to Agriculture and Residence Patterns 🌍.pptx
PPTX
coordinate systems map projections and graphical and atoms ppt group (B).pptx
PDF
Network Analysis using GIS Techniques navigation network mapping for transpor...
PDF
PDF
Application of Data Structures in GIS and the Purpose of Modeling
PPTX
đŸŒŸ Introduction to Agriculture and Residence Patterns 🌍.pptx
coordinate systems map projections and graphical and atoms ppt group (B).pptx
Network Analysis using GIS Techniques navigation network mapping for transpor...
Application of Data Structures in GIS and the Purpose of Modeling
Ad

Recently uploaded (20)

PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
L1 - Introduction to python Backend.pptx
PDF
medical staffing services at VALiNTRY
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Introduction to Artificial Intelligence
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
System and Network Administration Chapter 2
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Operating system designcfffgfgggggggvggggggggg
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
L1 - Introduction to python Backend.pptx
medical staffing services at VALiNTRY
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
ManageIQ - Sprint 268 Review - Slide Deck
Design an Analysis of Algorithms I-SECS-1021-03
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Materi_Pemrograman_Komputer-Looping.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
How to Choose the Right IT Partner for Your Business in Malaysia
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Upgrade and Innovation Strategies for SAP ERP Customers
Introduction to Artificial Intelligence
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
System and Network Administration Chapter 2
Design an Analysis of Algorithms II-SECS-1021-03
How to Migrate SBCGlobal Email to Yahoo Easily
Operating system designcfffgfgggggggvggggggggg

Object-Oriented Programming (OO) in pythonP) in python.pptx

  • 1. Object-Oriented Programming (OOP) in python BAKHAT ALI Institute of Geoinformatics and Earth Observation, Pir Mehr Ali Shah Arid Agriculture University Rawalpindi , Punjab, Pakistan bakhtali21uaar@gmail.com
  • 2. Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to encapsulate data and functionality, promoting organized code and efficient data reuse.
  • 3. Key Features of OOP Encapsulation: Protects and bundles data and methods together, ensuring outside interference is limited. Abstraction: Simplifies complex systems by exposing only essential features while hiding unnecessary details. Inheritance: Allows one class to inherit attributes and behaviors from another, enabling code reuse and creating a class hierarchy. Polymorphism: Grants objects the ability to be treated as instances of their parent class, while allowing their specific implementations to define how actions are executed.
  • 4. Classes and Objects Classes A class is a blueprint to create objects, comprising attributes (data) and methods (functions). Objects An object is an instantiation of a class, representing a specific instance with defined attributes.
  • 5. Class and Object Syntax class ClassName: # Define a class def __init__(self, param): # Constructor self.attribute = param # Instance variable def method_name(self): # Method definition return self.attribute # Creating an object (instantiation) object_instance = ClassName(value) Example of Class and Object class Car: def __init__(self, make, model, year): self.make = make self.model = model self.year = year def display_info(self): return f"{self.year} {self.make} {self.model}" # Instantiating the object my_car = Car("Toyota", "Corolla", 2021) print(my_car.display_info()) # Output: 2021 Toyota Corolla
  • 6. . Inheritance Inheritance allows one class (child class) to inherit the properties and methods of another (parent class).
  • 7. Types of Inheritance SINGLE INHERITANCE: A CHILD CLASS INHERITS FROM ONE PARENT CLASS. class Animal: def sound(self): return "Some sound" class Dog(Animal): def sound(self): return "Bark" dog = Dog() print(dog.sound()) # Output: Bark
  • 8. Multilevel Inheritance: A class inherits from a derived class. CLASS PUPPY(DOG): DEF SOUND(SELF): RETURN "YIP" PUPPY = PUPPY() PRINT(PUPPY.SOUND()) # OUTPUT: YIP
  • 9. Hierarchical Inheritance: Multiple classes inherit from one parent class. class Vehicle: def type(self): return "Vehicle" class Car(Vehicle): def type(self): return "Car" class Bike(Vehicle): def type(self): return "Bike" car = Car() bike = Bike() print(car.type()) # Output: Car print(bike.type()) # Output: Bike
  • 10. Multiple Inheritance: A class can inherit from multiple parent classes. class A: def method_a(self): return "Method A" class B: def method_b(self): return "Method B" class C(A, B): def method_c(self): return "Method C" obj = C() print(obj.method_a()) # Output: Method A print(obj.method_b()) # Output: Method B
  • 11. Polymorphism POLYMORPHISM ALLOWS METHODS TO DO DIFFERENT THINGS BASED ON THE OBJECT TYPE OR CLASS INVOKING THEM.
  • 12. Compile-time Polymorphism (Static Binding): Achieved mainly through method overloading. lass Math: def add(self, a, b): return a + b def add_with_three(self, a, b, c): return a + b + c math = Math() print(math.add(5, 10)) # Output: 15 print(math.add_with_three(5, 10, 15)) # Output: 30
  • 13. Run-time Polymorphism (Dynamic Binding): Achieved through method overriding. lass Shape: def area(self): pass class Rectangle(Shape): def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * (self.radius**2) shapes = [Rectangle(10, 5), Circle(7)] for shape in shapes: print(shape.area()) # Output: 50, 153.86
  • 14. Encapsulation ENCAPSULATION COMBINES ATTRIBUTES AND METHODS IN A CLASS AND RESTRICTS ACCESS USING ACCESS MODIFIERS.
  • 15. class BankAccount: def __init__(self, balance): self.__balance = balance # Private attribute def deposit(self, amount): self.__balance += amount def withdraw(self, amount): if amount <= self.__balance: self.__balance -= amount else: print("Insufficient funds.") def get_balance(self): return self.__balance account = BankAccount(1000) account.deposit(500) print(account.get_balance()) # Output: 1500 account.withdraw(200) print(account.get_balance()) # Output: 1300
  • 16. Abstraction ABSTRACTION SIMPLIFIES COMPLEX SYSTEMS BY HIDING UNNECESSARY DETAILS AND EXPOSING ONLY THE NECESSARY PARTS USING ABSTRACT CLASSES.
  • 17. from abc import ABC, abstractmethod class Device(ABC): @abstractmethod def turn_on(self): pass class Television(Device): def turn_on(self): return "TV is now ON." class Radio(Device): def turn_on(self): return "Radio is now ON." tv = Television() radio = Radio() print(tv.turn_on()) # Output: TV is now ON. print(radio.turn_on()) # Output: Radio is now ON.
  • 18. Comprehensive OOP Features Table with Code Examples Feature Definition Syntax Example Code Example Class A blueprint for objects. class ClassName: class Vehicle: pass Object An instance of a class. object_name = ClassName() my_car = Vehicle() Attributes Data stored in an object. self.attribute_name = value self.make = "Toyota" Methods Functions defined inside a class. def method_name(self): def display_info(self): return self.make Constructor A method called when an object is created. def __init__(self, parameters): def __init__(self, make, model): self.make = make Encapsulation Restricting access to object data. Use of underscores: self.__private_data self.__balance = 1000 Inheritance Deriving new classes from existing ones. class DerivedClass(ParentClass): class Dog(Animal): Polymorphism Methods behaving differently based on the object. Method Overriding class Cat(Animal): def sound(self): return "Meow" Abstraction Hiding complex implementation details. Use of abstract methods from abc import ABC, abstractmethod
  • 19. OOP Summary: Encapsulation: Protects and bundles data/functionality. Abstraction: Simplifies interfaces. Inheritance: Supports code reuse and hierarchy. Polymorphism: Flexible interfaces for objects.