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:
4. Object-Oriented Programming methodologies:  Inheritance  Polymorphism  Encapsulation  Abstraction 7
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.
class employee1()://This is a parent class def init (self, name, age, salary): self.name = name self.age = age self.salary = salary class childemployee(employee1)://This is a child class def init (self, name, age, salary,id): self.name = name self.age = age self.salary = salary self.id = id emp1 = employee1('harshit',22,1000) print(emp1.age) 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.
19
Polymorphism is of two types: 20  Compile-time Polymorphism  Run-time Polymorphism
Compile-time Polymorphism: 21 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 22 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: 24 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") 25 Example:
class childemployee2(employee): def earn(self): print("has money") c = childemployee1 c.earn(employee) d = childemployee2 d.earn(employee) 26 Output: no money, has money
Abstraction: 27 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.

9-_Object_Oriented_Programming_Using_Python 1.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.
    4. Object-Oriented Programming methodologies:  Inheritance Polymorphism  Encapsulation  Abstraction 7
  • 8.
    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.
  • 9.
  • 10.
    Single Inheritance: Single levelinheritance enables a derived class to inherit characteristics from a single parent class.
  • 11.
    class employee1()://This isa parent class def init (self, name, age, salary): self.name = name self.age = age self.salary = salary class childemployee(employee1)://This is a child class def init (self, name, age, salary,id): self.name = name self.age = age self.salary = salary self.id = id emp1 = employee1('harshit',22,1000) print(emp1.age) Output: 22 Example:
  • 12.
    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):
  • 13.
    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
  • 14.
    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
  • 15.
    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)
  • 16.
    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
  • 17.
    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)
  • 18.
    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.
  • 19.
  • 20.
    Polymorphism is oftwo types: 20  Compile-time Polymorphism  Run-time Polymorphism
  • 21.
    Compile-time Polymorphism: 21 A compile-timepolymorphism also called polymorphism which gets resolved during the time of the program. One common example overloading” as static compilation is “method
  • 22.
    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 22 Example:
  • 23.
    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
  • 24.
    Run-time Polymorphism: 24 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”.
  • 25.
    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") 25 Example:
  • 26.
    class childemployee2(employee): def earn(self): print("hasmoney") c = childemployee1 c.earn(employee) d = childemployee2 d.earn(employee) 26 Output: no money, has money
  • 27.
    Abstraction: 27 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.