Professional Documents
Culture Documents
Unit 5
Unit 5
Unit 5
• 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
<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
# 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
t1 = Test("Kamalakar", "Unhalkar")
print(t1.firstName)
print(t1.lastName)
print(Test.firstName)
print(Test.lastName)
‘self’ Parameter
Java Python
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.
def displayData(self):
print(self.firstName)
print(self.lastName)
# This is derived class
class Employee(Person):
pass
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)