Unit 5

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

Unit-5

• Python Object Oriented Programming (03 Hrs.)

Features of OOP: classes, objects, methods and


message passing, inheritance, polymorphism,
containership, reusability, delegation, data
abstraction and encapsulation
Python OOPs Concepts

• Object.
• Class.
• Method.
• Inheritance.
• Polymorphism.
• Data Abstraction.
• Encapsulation.
• The object is an entity that has state and behavior. It may be
any real-world object like the mouse, keyboard, chair, table,
pen, etc.
• The class can be defined as a collection of objects. It is a
logical entity that has some specific attributes and methods.
• The method is a function that is associated with an object. In
Python, a method is not unique to class instances. Any object
type can have methods.
Syntax for creating classes

• The syntax for classes


– class <Identifier for the class> :
<Indentation> Data Members
Generally no variable is declared at class level in
Python

<Indentation> Constructor
<Indentation> Methods
# End of class declaration
class Person:
# Data members of the class
firstName = “ABC"
lastName = “XYZ"
# No constructor
# No methods
p1 = Person()
print(p1.firstName)
print(p1.lastName)
Python class
• Data Members are accessible on the object
• It looks like that the data members are public
class Person:
# Data members of the class
firstName = “ABC"
lastName = “XYZ"

print(Person.firstName)
print(Person.lastName)
Python Class
• Data Members are accessible on the name of
the class

• It looks like that the data members are public


and static
class Person:
# Data members of the class
firstName = “ABC"
lastName = “XYZ"

# No constructor
# No methods

p1 = Person()
print("Id of object", id(p1))
print("Id of firstName", id(p1.firstName))
print("Id of lastName", id(p1.lastName))
Constructor
• The data member should be initialized at the
time of object creation

• They can be changed as and when required at


later time
Python class constructor
• The signature of python constructor
__init__(self, variable1, variable 2)

• It is two times underscore


• The self is by default, but it can be anything and
need not be self
• It can take any number of variables
• Declare the data members of the class in
constructor
Python class constructor
class Test:
def __init__(self, firstName, lastName):
self.firstName = firstName
self.lastName = lastName

t1 = Test("Kamalakar", "Unhalkar")
print(t1.firstName)
print(t1.lastName)

print(Test.firstName)
print(Test.lastName)
‘self’ Parameter
Java Python

• ‘this’ is used to reference to • ‘self’ is used to reference to


the current instance of the the current instance of the
class class

• It is used to access variables • It is used to access variables


that are data members of that belongs to the instance
the instance of the class of the class.

• It should be word ‘this’ only


• It does not have to be
named self. It can be any
word
• It is not required to be
• It is not required to be
passed as function
passed as function
parameters
parameters
Python Class Method
class Test:
nativePlace = “Goa”

def __init__(self, firstName, lastName):


self.firstName = firstName
self.lastName = lastName

def displayData(self):
print(self.firstName)
print(self.lastName)
t1 = Test("Kamalakar", "Unhalkar")
t1.displayData()

t2 = Test(“AAAA”, “BBBB”)
t2.displayData()

print(t1.nativePlace)
print(t2.nativePlace)
ENCAPSULATION
Encapsulation in Python Classes
• Private attributes are not protected by the Python
system. That is by design decision.

• Private attributes will be masked. The reason is, that


there should be no clashes in the inheritance chain.

• The masking is done by some implicit renaming.


Private attributes will have the real name
"__<className>_<attributeName>"
INHERITANCE
Python Multi-Level inheritance
# This is base class
class Person:
def __init__(self, fn, ln):
self.firstName = fn
self.lastName = ln

def displayData(self):
print(self.firstName)
print(self.lastName)
# This is derived class
class Employee(Person):
pass

# Use of derived class


e = Employee("Kamalakar", "Unhalkar")
e.displayData()
# This is base class
class Person:
def __init__(self, fn, ln):
self.firstName = fn
self.lastName = ln

def displayData(self):
print(self.firstName)
print(self.lastName)
# This is derived class
class Employee(Person):
def __init__(self, fn, ln, eid):
Person.__init__(self, fn, ln)
self.employeeId = eid

def displayData(self):
print(self.firstName)
print(self.lastName)
print(self.employeeId)
# Use of derived class
e = Employee("Kamalakar", "Unhalkar", 12345)
e.displayData()
Python Multiple inheritance
Python Multiple inheritance
POLYMORPHISM
# This is derived class BankCustomer
class BankCustomer(Person):
def __init__(self, fn, ln, bac):
Person.__init__(self, fn, ln)
self.bankAccountNumber = bac

def displayData(self):
print(self.firstName)
print(self.lastName)
print(self.bankAccountNumber)
# Use of derived class
p = Person("Kamalakar", "Unhalkar")
p.displayData()
p = Employee("Kamalakar", "Unhalkar", 12345)
p.displayData()
p = BankCustomer("Kamalakar", "Unhalkar",
5000067)
p.displayData()
obj_emp = Employee("Kamalakar", "Unhalkar",
12345)
obj_bank = BankCustomer("Kamalakar",
"Unhalkar", 5000067)
objList = [obj_emp, obj_bank]
for x in objList:
x.displayData()
ENCAPSULATION
• It describes the idea of wrapping data and the
methods that work on data within one unit.
• This puts restrictions on accessing variables and
methods directly and can prevent the accidental
modification of data.
• To prevent accidental change, an object’s variable can
only be changed by an object’s method. Those type of
variables are known as private variable.
• A class is an example of encapsulation as it
encapsulates all the data that is member functions,
variables, etc.
CONTAINERSHIP
• Containership also referred to as composition allows a
class to contain an object of a different class as a member
data.
• If a class is contained in another, the container does not
get the ability to change or add behavior to the contained.
• Containership represents a “has-a” relationship, In simple
terms it means a class been called in another class.
Eg. Class A
{…….} Obj1;
Class B
{
A obj1;
}
DELEGATION
• Delegation is an object oriented technique also called a
design pattern.
• Python programmers can easily implement delegation. For
example, the following class implements a class that behaves
like a file but converts all written data to uppercase:
• Eg.
class UpperOut:
    def __init__(self, outfile):
        self._outfile = outfile
    def write(self, s):
        self._outfile.write(s.upper())
    def __getattr__(self, name):
        return getattr(self._outfile, name)

You might also like