Object Oriented
Programming Using
Python
1
1. Introduction to Object Oriented Programming in Python
2. Difference between object and procedural oriented
programming
3. What are Classes and Objects?
4. Object-Oriented Programming methodologies:
• Inheritance
• Polymorphism
• Encapsulation
• Abstraction
2
Index
1. Introduction to Object
Oriented Programming in
Python
3
Object Oriented Programming is a way of
computer programming using the idea of
“objects” to represents data and methods. It is
also, an approach used for creating neat and
reusable code instead of a redundant one.
2. Difference between Object-Oriented
and Procedural Oriented
Programming
Object-Oriented Programming (OOP)
Procedural-Oriented Programming
(Pop)
It is a bottom-up approach It is a top-down approach
Program is divided into objects Program is divided into functions
Makes use of Access modifiers
‘public’, private’, protected’
Doesn’t use Access modifiers
It is more secure It is less secure
Object can move freely within member
functions
Data can move freely from function
to function within programs
It supports inheritance It does not support inheritance
class class1: // class 1 is the name of the class
5
blueprint of objects defining the common attributes
A class is a collection of objects or you can say it is a
and
behavior. Now the question arises, how do you do that?
Class is defined under a “Class” Keyword.
Example:
3. What are Classes and Objects?
Example:
class employee:
def init (self,name,age,id,salary): //creating a
function self.name = name // self is an instance of a class
self.age = age
self.salary = salary
self.id = id
emp1 =
employee("harshit",22,
1000,1234) //creating
objects
emp2 =
employee("arjun",23,2
000,2234)
6
Creating an Object and Class in python:
What is `__init__`?
• ✅ `__init__` is a special method (constructor) in Python classes.
• ✅ It initializes instance attributes when an object is created.
• ✅ It is automatically called when a new object is instantiated.
• ✅ It must have double underscores before and after
(`__init__`).
Incorrect: Using `init` Instead of `__init__`
• ❌ `def init(self, ...)` is incorrect.
• ❌ It will not be automatically called when an object is created.
• Example of Incorrect Code:
• class Test:
• def init(self, name):
• self.name = name
• obj = Test('Alice') # ERROR: init() is never called
• print(obj.name) # Will raise an AttributeError
Correct: Using `__init__`
• **Example of Correct Code:**
• class Test:
• def __init__(self, name):
• self.name = name
• obj = Test('Alice') # Now __init__ is called automatically
• print(obj.name) # Output: Alice
• ✅ This ensures proper object initialization.
Key Takeaways
• ✅ `__init__` is the constructor method in Python.
• ✅ It initializes instance attributes when an object is created.
• ✅ Must use double underscores (`__init__`), not `init`.
• ✅ It ensures proper initialization of objects.
What is 'self' in Python?
• 'self' is a reference to the current instance of a class.
• It is used to access instance variables and methods.
• It differentiates instance attributes from local variables.
Why is 'self' Needed?
• 1. 'self' helps differentiate instance variables from local
variables.
• 2. It ensures each object of a class has its own unique data.
• 3. It is necessary for accessing instance attributes inside
methods.
Example of 'self' Usage in a Class
• class Employee:
• def __init__(self, name, age):
• self.name = name
• self.age = age
• def display_info(self):
• print(f'Name: {self.name}, Age: {self.age}')
• # Creating objects
• emp1 = Employee('Alice', 25)
• emp2 = Employee('Bob', 30)
• emp1.display_info() # Output: Name: Alice, Age: 25
• emp2.display_info() # Output: Name: Bob, Age: 30
Key Takeaways
• ✅ 'self' represents the current instance of the class.
• ✅ It must be the first parameter in instance methods.
• ✅ It allows each instance to have its own attributes.
• ✅ It is passed automatically when calling methods on an object.
4. Object-Oriented
Programming
methodologies:
 Inheritance
 Polymorphism
 Encapsulation
 Abstraction
1
5
Inheritance:
Ever heard of this dialogue from relatives “you look exactly
like your father/mother” the reason behind this is called
‘inheritance’. From the Programming aspect, It generally
means “inheriting or transfer of characteristics from parent to
child class without any modification”. The new class is called
the derived/child class and the one from which it is derived is
called a parent/base class.
9
Single Inheritance:
Single level inheritance enables a derived class to inherit
characteristics from a single parent class.
# Parent class
class Employee1:
def __init__(self, name, age, salary): # Corrected '__init__' method
self.name = name
self.age = age
self.salary = salary
# Child class inheriting from Employee1
class ChildEmployee(Employee1):
def __init__(self, name, age, salary, id): # Corrected '__init__' method
super().__init__(name, age, salary) # Calling parent class constructor
self.id = id
# Creating an object of Employee1
emp1 = Employee1('harshit', 22, 1000)
print(emp1.age)
# Expected Output: 22
Example:
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.
Example:
class employee://Super class
def init
(self,name,age,salary):
self.name = name
self.age = age
self.salary = salary
class
childemployee1(employe
e)://First child class
def init
(self,name,age,salary):
class childemployee2(childemployee1)://Second child class
def init (self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
emp1 =
employee('harshit',22,10
00)
emp2 =
childemployee1('arjun',2
3,2000)
print(emp1.age)
print(emp2.age)
Output: 22,23
Hierarchical Inheritance:
Hierarchical level inheritance enables more than one derived
class to inherit properties from a parent class.
Example:
//Hierarchical Inheritance
class employee:
def init (self, name, age,
salary): self.name = name
self.age = age
self.salary = salary
class childemployee1(employee):
def init
(self,name,age,salary):
self.name = name
self.age = age
self.salary = salary
class
childemployee2(employe
e):
def init (self, name, age,
salary): self.name = name
self.age = age
self.salary = salary
emp1 = employee('harshit',22,1000)
emp2 = employee('arjun',23,2000)
Multiple Inheritance:
Multiple level inheritance enables one derived class to inherit
properties from more than one base class.
Example:
class employee1: //Parent class
def init (self, name, age,
salary): self.name = name
self.age = age
self.salary = salary
class employee2(): //Parent class
def init (self,name,age,salary,id):
self.name = name
self.age = age
self.salary = salary
self.id = id
class childemployee(employee1,employee2):
def init (self, name, age, salary,id):
self.name = name
self.age = age
self.salary = salary
self.id = id
emp1 = employee1('harshit',22,1000)
emp2 = employee2('arjun',23,2000,1234)
Polymorphism:
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’. It is
one such OOP methodology where one task can be performed
in several different ways. To put it in simple words, it is
a property of an object which allows it to take multiple forms.
Polymorphism is of two types:
27
 Compile-time Polymorphism
 Run-time Polymorphism
Compile-time Polymorphism:
28
A compile-time polymorphism also called
polymorphism which gets resolved during the
time of the program. One common example
overloading”
as static
compilation
is “method
class employee1():
def name(self):
print("Harshit
is his
name")
def salary(self):
print("3000 is his salary")
def age(self):
print("22 is his age")
class
employee2()
:
def
name(self):
print("Rahul is his name")
def salary(self):
print("4000 is his
29
Example:
23
def func(obj)://Method Overloading
obj.name()
obj.salary()
obj.age()
obj_emp1 = employee1()
obj_emp2 = employee2()
func(obj_emp1)
func(obj_emp2)
Output:
Harshit is his name
3000 is his salary
22 is his age
Rahul is his name
4000 is his salary
23 is his age
Run-time Polymorphism:
31
A run-time Polymorphism is also, called as dynamic
polymorphism where it gets resolved into the run time. One
common example of Run-time polymorphism is “method
overriding”.
class employee():
def init
(self,name,age,id,salary):
self.name = name
self.age = age
self.salary = salary
self.id = id
def earn(self):
pass
class
childemployee1(e
mployee):
def earn(self): //Run-time polymorphism
print("no money")
32
Example:
class childemployee2(employee):
def earn(self):
print("has money")
c = childemployee1
c.earn(employee)
d = childemployee2
d.earn(employee)
33
Output: no money, has money
Abstraction:
34
Suppose you booked a movie ticket from bookmyshow using
net banking or any other process. You don’t know the
procedure of how the pin is generated or how the verification
is done. This is called ‘abstraction’ from the programming
aspect, it basically means you only show the implementation
details of a particular process and hide the details from the
user.
Introduction to Constructors and Destructors
• • Constructors and destructors are special methods in Python
classes.
• • They help in initializing and cleaning up objects.
• • Essential for managing resources effectively in object-
oriented programming.
Constructors in Python
• • A constructor is a special method called __init__().
• • It is automatically executed when an object is created.
• • Used to initialize object attributes.
• Example:
• class Student:
• def __init__(self, name, age):
• self.name = name
• self.age = age
Destructors in Python
• • A destructor is a special method called __del__().
• • It is automatically executed when an object is deleted.
• • Used to clean up resources (e.g., closing files, releasing memory).
• Example:
• class Student:
• def __del__(self):
• print('Destructor called, object deleted')
Key Differences
• • __init__() is called when an object is created, __del__() when
it is destroyed.
• • Constructor is used to initialize attributes, destructor is used
for cleanup.
• • __del__() is called automatically when an object is out of
scope.
Conclusion
• • Constructors and destructors are crucial for object lifecycle
management.
• • __init__() helps set up objects, while __del__() helps clean
up resources.
• • Understanding these concepts enhances memory
management and performance in Python.

Presentation_4516_Content_Document_20250204010703PM.pptx

  • 1.
  • 2.
    1. Introduction toObject Oriented Programming in Python 2. Difference between object and procedural oriented programming 3. What are Classes and Objects? 4. Object-Oriented Programming methodologies: • Inheritance • Polymorphism • Encapsulation • Abstraction 2 Index
  • 3.
    1. Introduction toObject Oriented Programming in Python 3 Object Oriented Programming is a way of computer programming using the idea of “objects” to represents data and methods. It is also, an approach used for creating neat and reusable code instead of a redundant one.
  • 4.
    2. Difference betweenObject-Oriented and Procedural Oriented Programming Object-Oriented Programming (OOP) Procedural-Oriented Programming (Pop) It is a bottom-up approach It is a top-down approach Program is divided into objects Program is divided into functions Makes use of Access modifiers ‘public’, private’, protected’ Doesn’t use Access modifiers It is more secure It is less secure Object can move freely within member functions Data can move freely from function to function within programs It supports inheritance It does not support inheritance
  • 5.
    class class1: //class 1 is the name of the class 5 blueprint of objects defining the common attributes A class is a collection of objects or you can say it is a and behavior. Now the question arises, how do you do that? Class is defined under a “Class” Keyword. Example: 3. What are Classes and Objects?
  • 6.
    Example: class employee: def init(self,name,age,id,salary): //creating a function self.name = name // self is an instance of a class self.age = age self.salary = salary self.id = id emp1 = employee("harshit",22, 1000,1234) //creating objects emp2 = employee("arjun",23,2 000,2234) 6 Creating an Object and Class in python:
  • 7.
    What is `__init__`? •✅ `__init__` is a special method (constructor) in Python classes. • ✅ It initializes instance attributes when an object is created. • ✅ It is automatically called when a new object is instantiated. • ✅ It must have double underscores before and after (`__init__`).
  • 8.
    Incorrect: Using `init`Instead of `__init__` • ❌ `def init(self, ...)` is incorrect. • ❌ It will not be automatically called when an object is created. • Example of Incorrect Code: • class Test: • def init(self, name): • self.name = name • obj = Test('Alice') # ERROR: init() is never called • print(obj.name) # Will raise an AttributeError
  • 9.
    Correct: Using `__init__` •**Example of Correct Code:** • class Test: • def __init__(self, name): • self.name = name • obj = Test('Alice') # Now __init__ is called automatically • print(obj.name) # Output: Alice • ✅ This ensures proper object initialization.
  • 10.
    Key Takeaways • ✅`__init__` is the constructor method in Python. • ✅ It initializes instance attributes when an object is created. • ✅ Must use double underscores (`__init__`), not `init`. • ✅ It ensures proper initialization of objects.
  • 11.
    What is 'self'in Python? • 'self' is a reference to the current instance of a class. • It is used to access instance variables and methods. • It differentiates instance attributes from local variables.
  • 12.
    Why is 'self'Needed? • 1. 'self' helps differentiate instance variables from local variables. • 2. It ensures each object of a class has its own unique data. • 3. It is necessary for accessing instance attributes inside methods.
  • 13.
    Example of 'self'Usage in a Class • class Employee: • def __init__(self, name, age): • self.name = name • self.age = age • def display_info(self): • print(f'Name: {self.name}, Age: {self.age}') • # Creating objects • emp1 = Employee('Alice', 25) • emp2 = Employee('Bob', 30) • emp1.display_info() # Output: Name: Alice, Age: 25 • emp2.display_info() # Output: Name: Bob, Age: 30
  • 14.
    Key Takeaways • ✅'self' represents the current instance of the class. • ✅ It must be the first parameter in instance methods. • ✅ It allows each instance to have its own attributes. • ✅ It is passed automatically when calling methods on an object.
  • 15.
    4. Object-Oriented Programming methodologies:  Inheritance Polymorphism  Encapsulation  Abstraction 1 5
  • 16.
    Inheritance: Ever heard ofthis dialogue from relatives “you look exactly like your father/mother” the reason behind this is called ‘inheritance’. From the Programming aspect, It generally means “inheriting or transfer of characteristics from parent to child class without any modification”. The new class is called the derived/child class and the one from which it is derived is called a parent/base class.
  • 17.
  • 18.
    Single Inheritance: Single levelinheritance enables a derived class to inherit characteristics from a single parent class.
  • 19.
    # Parent class classEmployee1: def __init__(self, name, age, salary): # Corrected '__init__' method self.name = name self.age = age self.salary = salary # Child class inheriting from Employee1 class ChildEmployee(Employee1): def __init__(self, name, age, salary, id): # Corrected '__init__' method super().__init__(name, age, salary) # Calling parent class constructor self.id = id # Creating an object of Employee1 emp1 = Employee1('harshit', 22, 1000) print(emp1.age) # Expected Output: 22 Example:
  • 20.
    Multilevel Inheritance: Multi-level inheritanceenables a derived class to inherit properties from an immediate parent class which in turn inherits properties from his parent class. Example: class employee://Super class def init (self,name,age,salary): self.name = name self.age = age self.salary = salary class childemployee1(employe e)://First child class def init (self,name,age,salary):
  • 21.
    class childemployee2(childemployee1)://Second childclass def init (self, name, age, salary): self.name = name self.age = age self.salary = salary emp1 = employee('harshit',22,10 00) emp2 = childemployee1('arjun',2 3,2000) print(emp1.age) print(emp2.age) Output: 22,23
  • 22.
    Hierarchical Inheritance: Hierarchical levelinheritance enables more than one derived class to inherit properties from a parent class. Example: //Hierarchical Inheritance class employee: def init (self, name, age, salary): self.name = name self.age = age self.salary = salary
  • 23.
    class childemployee1(employee): def init (self,name,age,salary): self.name= name self.age = age self.salary = salary class childemployee2(employe e): def init (self, name, age, salary): self.name = name self.age = age self.salary = salary emp1 = employee('harshit',22,1000) emp2 = employee('arjun',23,2000)
  • 24.
    Multiple Inheritance: Multiple levelinheritance enables one derived class to inherit properties from more than one base class. Example: class employee1: //Parent class def init (self, name, age, salary): self.name = name self.age = age self.salary = salary
  • 25.
    class employee2(): //Parentclass def init (self,name,age,salary,id): self.name = name self.age = age self.salary = salary self.id = id class childemployee(employee1,employee2): def init (self, name, age, salary,id): self.name = name self.age = age self.salary = salary self.id = id emp1 = employee1('harshit',22,1000) emp2 = employee2('arjun',23,2000,1234)
  • 26.
    Polymorphism: You all musthave 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’. It is one such OOP methodology where one task can be performed in several different ways. To put it in simple words, it is a property of an object which allows it to take multiple forms.
  • 27.
    Polymorphism is oftwo types: 27  Compile-time Polymorphism  Run-time Polymorphism
  • 28.
    Compile-time Polymorphism: 28 A compile-timepolymorphism also called polymorphism which gets resolved during the time of the program. One common example overloading” as static compilation is “method
  • 29.
    class employee1(): def name(self): print("Harshit ishis name") def salary(self): print("3000 is his salary") def age(self): print("22 is his age") class employee2() : def name(self): print("Rahul is his name") def salary(self): print("4000 is his 29 Example:
  • 30.
    23 def func(obj)://Method Overloading obj.name() obj.salary() obj.age() obj_emp1= employee1() obj_emp2 = employee2() func(obj_emp1) func(obj_emp2) Output: Harshit is his name 3000 is his salary 22 is his age Rahul is his name 4000 is his salary 23 is his age
  • 31.
    Run-time Polymorphism: 31 A run-timePolymorphism is also, called as dynamic polymorphism where it gets resolved into the run time. One common example of Run-time polymorphism is “method overriding”.
  • 32.
    class employee(): def init (self,name,age,id,salary): self.name= name self.age = age self.salary = salary self.id = id def earn(self): pass class childemployee1(e mployee): def earn(self): //Run-time polymorphism print("no money") 32 Example:
  • 33.
    class childemployee2(employee): def earn(self): print("hasmoney") c = childemployee1 c.earn(employee) d = childemployee2 d.earn(employee) 33 Output: no money, has money
  • 34.
    Abstraction: 34 Suppose you bookeda movie ticket from bookmyshow using net banking or any other process. You don’t know the procedure of how the pin is generated or how the verification is done. This is called ‘abstraction’ from the programming aspect, it basically means you only show the implementation details of a particular process and hide the details from the user.
  • 35.
    Introduction to Constructorsand Destructors • • Constructors and destructors are special methods in Python classes. • • They help in initializing and cleaning up objects. • • Essential for managing resources effectively in object- oriented programming.
  • 36.
    Constructors in Python •• A constructor is a special method called __init__(). • • It is automatically executed when an object is created. • • Used to initialize object attributes. • Example: • class Student: • def __init__(self, name, age): • self.name = name • self.age = age
  • 37.
    Destructors in Python •• A destructor is a special method called __del__(). • • It is automatically executed when an object is deleted. • • Used to clean up resources (e.g., closing files, releasing memory). • Example: • class Student: • def __del__(self): • print('Destructor called, object deleted')
  • 38.
    Key Differences • •__init__() is called when an object is created, __del__() when it is destroyed. • • Constructor is used to initialize attributes, destructor is used for cleanup. • • __del__() is called automatically when an object is out of scope.
  • 39.
    Conclusion • • Constructorsand destructors are crucial for object lifecycle management. • • __init__() helps set up objects, while __del__() helps clean up resources. • • Understanding these concepts enhances memory management and performance in Python.