9 - Object Oriented Programming Using Python
9 - Object Oriented Programming Using Python
9 - Object Oriented Programming Using Python
Programming Using
Python
1
Index
2
1. Introduction to Object Oriented
Programming in Python
3
2. Difference between Object-Oriented and
Procedural Oriented Programming
Procedural-Oriented Programming
Object-Oriented Programming (OOP)
(Pop)
Object can move freely within member Data can move freely from function to
functions function within programs
Example:
5
Creating an Object and Class in python:
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
6
4. Object-Oriented Programming
methodologies:
❑ Inheritance
❑ Polymorphism
❑ Encapsulation
❑ Abstraction
7
Inheritance:
Output: 22
Multilevel Inheritance:
Example:
print(emp1.age)
print(emp2.age)
Output: 22,23
Hierarchical Inheritance:
Example:
class employee():
def __init__(self, name, age, salary): //Hierarchical Inheritance
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(employee):
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:
Example:
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.
19
Polymorphism is of two types:
❑ Compile-time Polymorphism
❑ Run-time Polymorphism
20
Compile-time Polymorphism:
21
Example:
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 salary")
def age(self):
print("23 is his age")
22
def func(obj)://Method Overloading
obj.name()
obj.salary()
obj.age()
obj_emp1 = employee1()
obj_emp2 = employee2()
func(obj_emp1)
func(obj_emp2)
Output:
24
Example:
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(employee):
def earn(self): //Run-time polymorphism
print("no money")
25
class childemployee2(employee):
def earn(self):
print("has money")
c = childemployee1
c.earn(employee)
d = childemployee2
d.earn(employee)
26
Abstraction:
27