0% found this document useful (0 votes)
3 views

python-4-OOPs

The document discusses the differences between Procedural and Object-Oriented Programming (OOP), highlighting key features and concepts of OOP in Python, such as classes, objects, encapsulation, inheritance, and polymorphism. It explains the structure of classes and methods, the significance of the 'self' parameter, and the automatic memory management in Python. Additionally, it provides sample code to illustrate the implementation of OOP principles.

Uploaded by

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

python-4-OOPs

The document discusses the differences between Procedural and Object-Oriented Programming (OOP), highlighting key features and concepts of OOP in Python, such as classes, objects, encapsulation, inheritance, and polymorphism. It explains the structure of classes and methods, the significance of the 'self' parameter, and the automatic memory management in Python. Additionally, it provides sample code to illustrate the implementation of OOP principles.

Uploaded by

lemniscatetools
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

7/22/20 By – Jai Mishra 1

14
Contents
> Differences Procedure vs Object Oriented
Programming
> Features of OOP
> Fundamental Concepts of OOP in Python
> What is Class
> What is Object
> Methods in Classes
Instantiating objects with init
self
> Encapsulation
> Data
Abstraction
> Public,
Protected and
Private Data
> Inheritance
7/22/20 2
14
Difference between Procedure Oriented and
Object Oriented Programming
 Procedural programming creates a step by step program that
guides the application through a sequence of instructions. Each
instruction is executed in order.
 Procedural programming also focuses on the idea that all
algorithms are executed with functions and data that the
programmer has access to and is able to change.
 Object-Oriented programming is much more similar to the way
the real world works; it is analogous to the human brain. Each
program is made up of many entities called objects.
 Instead, a message must be sent requesting the data, just like
people must ask one another for information; we cannot see
inside each other’s heads.
7/22/20 3
14
Summarized the differences
Procedure Oriented Programming Object Oriented Progrmming
In POP, program is divided into small In OOP, program is divided into parts
parts called functions called objects.
POP follows Top Down approach OOP follows Bottom Up approach
POP does not have any proper way for OOP provides Data Hiding so provides more
hiding data so it is less secure. security
In POP, Overloading is not possible In OOP, overloading is possible in the form
of Function Overloading and Operator
Overloading
In POP, Most function uses Global In OOP, data can not move easily from
data for sharing that can be accessed function to function, it can be kept public
freely from function to function in the or private so we can control the access of
system. data.
POP does not have any access specifier OOP has access specifiers named Public,
Private, Protected, etc.

7/22/20 4
14
Featuers of OOP
 Ability to simulate real-world event much more effectively
 Code is reusable thus less code may have to be written
 Data becomes active
 Better able to create GUI (graphical user interface) applications
 Programmers are able to produce faster, more accurate and better-
written applications

7/22/20 5
14
Fundamental concepts of OOP in Python
The four major principles of object orientation are:
Encapsulation (Closing data in a shell like classes)

Data Abstraction (designing scope of variable and


functions using void/static etc)

Inheritance

Polymorphism

7/22/20 6
14
What is an Object..?
 Objects are the basic run-time entities in an object-
oriented
system.
 They may represent a person, a place, a bank account, a table of
data or any item that the program must handle.
 When a program is executed the objects interact by
sending
messages to one another.
 Objects have two components:
- Data (i.e., attributes)
- Behaviors (i.e., methods)

7/22/20 7
14
Object Attributes and Methods Example
Object Attributes Object Methods
Store the data for that object Define the behaviors for the
Example (taxi): object
Driver Example (taxi):
OnDuty - PickUp
NumPassengers - DropOff
Location - GoOnDuty
- GoOffDuty
- GetDriver
- SetDriver
- GetNumPassengers
7/22/20 8
14
What is a Class..?
 A class is a special data type which defines how to build a certain
kind of object.
 The class also stores some data items that are shared by all the
instances of this class
 Instances are objects that are created which follow the definition
given inside of the class
 Python doesn’t use separate class interface definitions as in some
languages
 You just define the class and then use it

7/22/20 9
14
Methods in Classes
 Define a method in a class by including function definitions
within the scope of the class block
 There must be a special first argument self in all of method
definitions which gets bound to the calling instance
 There is usually a special method called init in
most classes

7/22/20 1
14 0
A Simple Class def: Student
class student:
“““A class representing a student ”””
def init (self
, n, a):
self.full_name = n
defself.age =a
get_age(self): #Method
return self.age
 Define class:
 Class name, begin with capital letter, by convention
 object: class based on (Python built-in type)
 Define a method
 Like defining a function
 Must have a special first parameter, self, which provides way
for a method to refer to object itself
7/22/20 1
14 1
Instantiating Objects with ‘ init
 ’init is the default constructor
 init serves as a constructor for the class.
Usually does some initialization work
An init method can take any number
of arguments
However, the first argument self in the
definition of
init is special

7/22/20 1
14 2
Self
 The first argument of every method is a reference to the current
instance of the class
 By convention, we name this argument self
 In init , self refers to the object currently being created; so, in
other class methods, it refers to the instance whose method was
called
 Similar to the keyword this in Java or C++
 But Python uses self more often than Java uses this
 You do not give a value for this parameter(self) when you call the
method, Python will provide it.
Continue…

7/22/20 VYBHAVA 1
14 TECHNOLOGIES 3
…Continue

 Although you must specify self explicitly when defining the


method, you don’t include it when calling the method.
 Python passes it for you automatically

Defining a method: Calling a method:


(this code inside a class definition.)
def get_age(self, num): >>> x.get_age(23)
self.age = num

7/22/20 VYBHAVA 1
14 TECHNOLOGIES 4
Deleting instances: No Need to “free”
 When you are done with an object, you don’t have to delete
or free it explicitly.
 Python has automatic garbage collection.
 Python will automatically detect when all of the references to
a piece of memory have gone out of scope. Automatically
frees that memory.
 Generally works well, few memory leaks
 There’s also no “destructor” method for classes

7/22/20 VYBHAVA 1
14 TECHNOLOGIES 5
Syntax for accessing attributes and methods
>>> f = student(“Python”, 14)

>>> f.full_name # Access attribute


“Python”

>>> f.get_age() # Access a method


14

7/22/20 VYBHAVA 1
14 TECHNOLOGIES 6
Simple Program for Class and Object
class Student:
# Initializing the variables
def init (self,name,age):
self.fullname=name
self.sage=age
# Display the entered data such as Students name and age
def display(self):
print 'Student FullName: %s' %(self.fullname)
print 'Stuent Age: %d'%(self.sage)
# S1,S2 and S3 objects
# S1,S2 and S3 are three different student details and age
s1=Student('Python',14)
s1.display()
s2=Student('Jython',23)
s2.display()
s3=Student('Objects',45)
s3.display()

7/22/20 VYBHAVA 1
14 TECHNOLOGIES 7
Output for the Sample Code

7/22/20 VYBHAVA 1
14 TECHNOLOGIES 8
Inheritance
 Inheritance is a powerful feature in object oriented programming
 It refers to defining a new class with little or no modification to an
existing class.
 The new class is called derived (or child) class and the one from which
it inherits is called the base (or parent) class.
 Derived class inherits features from the base class, adding new features
to it.
This results into re-usability of code.
Syntax:
class Baseclass(Object):
body_of_base_class
class
DerivedClass(BaseClass):
body_of_derived_clas

7/22/20 VYBHAVA 1
14 TECHNOLOGIES 9
While designing a inheritance concept, following key pointes keep it
in mind
A sub type never implements less functionality than the super type
Inheritance should never be more than two levels deep
We use inheritance when we want to avoid redundant code.

7/22/20 VYBHAVA 2
14 TECHNOLOGIES 0
Two built-in functions isinstance() and issubclass() are used
to check inheritances.
 Function isinstance() returns True if the object is an instance
of the class or other classes derived from it.
 Each and every class in Python inherits from
the base class object.

7/22/20 VYBHAVA 2
14 TECHNOLOGIES 1
Sample code for Inheritance
class Person(object): def showme(self):
def init super(Employe,self).show()
(self,name,age): print 'Company Name: %s'%
self.name=name (self.company)
self.age=age print 'Employe Salary per Annum: %s' %(self.sal)
def show(self): print '\n'
print 'Person Name: %s' %
(self.name) empdict={‘guido':['45',‘PYTHON','500000'],
print 'Person Age: %s' % ‘van':['25',‘JYTHON','200000'],
(self.age) ’rossum':['35',‘ABC','400000']}
class Employe(Person): for key,value in empdict.iteritems():
def init print key,value
(self,name,age,company,sal): emp=Employe(key,value[0],value[1],value[2])
self.company=company emp.showme()
self.sal=sal
super(Employe,self).

init

7/22/20 (name,age) VYBHAVA 2


14 TECHNOLOGIES 2
Polymorphism
 Polymorphism in Latin word which made up of
‘ploy’
means many and ‘morphs’ means forms
 From the Greek , Polymorphism means many(poly)
shapes (morph)
 This is something similar to a word having several different
meanings depending on the context
 Generally speaking, polymorphism means that a method or
function is able to cope with different types of input.

7/22/20 VYBHAVA 2
14 TECHNOLOGIES 3
A simple word ‘Cut’ can have different meaning
depending where it is used

If any body says “Cut” to these people

Surgeon: The Surgeon


would begin to make an
incision

Hair Stylist: The Hair


Cu Stylist would begin to
t cut someone’s hair

Actor: The actor would


abruptly stop acting out
the current scene,
awaiting directional
guidance
7/22/20 VYBHAVA 2
14 TECHNOLOGIES 4
In OOP , Polymorphism is the characteristic of being able to
assign a different meaning to a particular symbol or operator in
different contexts specifically to allow an entity such as a
variable, a function or an object to have more than one form.

There are two kinds of Polymorphism


Overloading :
Two or more methods with different signatures
Overriding:
Replacing an inherited method with another
having the same
signature

7/22/20 VYBHAVA 2
14 TECHNOLOGIES 5
A Sample Program for Polymorphism
class Person:
def init (self, # Constructor of the class
name):
self.name = name # Abstract method, defined by convention only
defraise
designation(self):
NotImplementedError("Subclass must implement abstract method")
class Employe(Person):
def designation(self):
return 'Software Engineer‘
class Doctor(Person):
def designation(self):
return 'Cardiologist‘
class Student(Person):
def designation(self):
return 'Graduate
Engineer'
persons = [Employe('Guido Van Rossum'),Doctor('Chalapathi'),Student('Robert')]
for person in persons:
print person.name + ': ' + person.designation()

7/22/20 VYBHAVA 2
14 TECHNOLOGIES 6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy