Object oriented programming_1
Object oriented programming_1
(OOP)
Introduction to OOP
• Object Oriented Programming (OOP) is
a programming technique to create programs
based on the real world objects.
• The states and behaviours of an object are
represented as the member variables and
methods.
• In object oriented programming the programs
are organized around objects and data rather
than actions and logic
Introduction to OOP
• It is also, an approach used for creating
reusable code instead of a redundant one.
• The program is divided into self-contained
objects or several mini-programs.
• Every Individual object represents a different
part of the application having its own logic
and data to communicate within themselves.
Advantages of OOP over POP
Object-Oriented Programming (OOP) Procedural-Oriented Programming (POP)
an = PartyAnimal()
an.party()
Output:
Method
OOP Terminology
• Class variable: A variable that is shared by all
instances of a class. Class variables are defined
within a class but outside any of the class's
methods. Class variables are not used as frequently
as instance variables are.
• Instance variable: A variable that is defined inside a
method and belongs only to the current instance of
a class.
• Data member: Defines either a class variable or an
instance variable used to hold data associated with
a class and its objects.
OOP Terminology
• Instance: An individual object of a certain class.
Example: An object obj that belongs to a class
Circle, is an instance of the class Circle.
• Instantiation: The creation of an instance of a class.
• Method: Defines the term used for functions that
are part of a class. Even though function and
method essentially define the same element,
method is considered more specific because only
classes can have methods.
OOP Terminology
• Object: Defines a unique instance of a class.
The object contains all the methods and
properties of the original class. However, the
data for each object differs. The storage
locations are unique, even if the data is the
same.
Self variable
• Python self variable is used to bind the instance
of the class to the instance method.
• We have to explicitly declare it as the first
method argument to access the instance
variables and methods.
• This variable is used only with the instance
methods.
• Python self variable is not a reserved keyword.
But, it’s the best practice and convention to use
the variable name as “self” to refer to the
instance.
Example 2
class Dog:
#instance method
def bark(self,breed):
print('{} is barking.'.format(breed))
d = Dog() #instantiation
d.bark('Tommy') #Method call
Output:
Tommy is barking
Constructor
• A constructor is a special method that is used
to initialize the instance variables of a class.
• In the constructor, we create the instance
variables and initialize them with some
starting values.
• A constructor is called at the time of creating
an instance.
Example 3
class Dog:
def __init__(self): #constructor
self.breed = 'Tommy'
def bark(self):
print('{} is barking.'.format(self.breed))
d = Dog()
d.bark()
Output:
Tommy is barking
Example 4
class Dog:
def __init__(self, breed):
self.breed = breed
def bark(self):
print('{} is barking.'.format(self.breed))
d = Dog('Tommy')
d.bark()
Output:
Tommy is barking
Destructor
• Destructor is a method which destructs the
object by destroying members of class. This
method is executed when ever the object
removed from memory.
• A destructor is called at the time of destroying
an instance.
Example 5
class Dog:
def __init__(self, breed):
self.breed = breed
def bark(self):
print('{} is barking.'.format(self.breed))
def __del__(self):
print("Destructor")
d = Dog('Tommy')
d.bark()
Output:
Tommy is barking
Destructor
Class and Object Variables
• Class variables are shared by all objects
(instances) of that class.
• As there is only one copy of a class variable a
change of value of such a variable is reflected in
all the other instances as well.
• Object variables (also called instance variables).
These variables can usually have different values
for different objects.
• E.g. Two accounts have different account
numbers
Example 6:
class Account(object):
counter = 0
def __init__(self, holder, number, balance,credit_line=1500):
Account.counter += 1
self.__Holder = holder
self.__Number = number
self.__Balance = balance
self.__CreditLine = credit_line
def __del__(self):
Account.counter -= 1
>>> Account.counter
0
>>> a1 = Account("Homer Simpson", 2893002, 2325.21)
>>> Account.counter
1
>>> a2 = Account("Fred Flintstone", 2894117, 755.32)
>>> Account.counter
2
>>> a3 = a2
>>> Account.counter
2
>>> a4 = Account("Bill Gates", 2895007, 5234.32)
>>> Account.counter
3
>>> del a4
>>> Account.counter
2
>>> del a3
>>> Account.counter 2
>>> del a2
>>> Account.counter
1
Inheritance
• 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 base/parent class.
Single Inheritance
class Employee:
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
class PartTime(Employee):
def __init__(self, name, age, salary,iid):
super().__init__(name, age, salary)
self.iid = iid
def empName(self):
print("Employee Name "+self.name)
Em = PartTime('Rajesh',32,23432,43)
Em.empName()
Multilevel Inheritance
class Employee:
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
class PartTime(Employee):
def __init__(self, name, age, salary,iid):
self.name = name
self.age = age
self.salary = salary
self.iid = iid
class Intern(PartTime):
def __init__(self, name, age, salary, iid):
self.name = name
self.age = age
self.salary = salary
self.iid = iid
emp1 = Intern('harshit',22,1000,1)
emp2 = PartTime('arjun',23,2000,2)
print(emp1.age)
print(emp2.age)
Hierarchical Inheritance
class Employee:
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
class PartTime(Employee):
def __init__(self, name, age, salary,iid):
self.name = name
self.age = age
self.salary = salary
self.iid = iid
class FullTime(Employee):
def __init__(self, name, age, salary, iid):
self.name = name
self.age = age
self.salary = salary
self.iid = iid
emp1 = FullTime('harshit',22,1000,1)
emp2 = PartTime('arjun',23,2000,2)
print(emp1.age)
print(emp2.age)
Multiple Inheritance
class employee():
def __init__(self, name, age,):
self.name = name
self.age = age
class payslip():
def __init__(self,da,age,salary):
self.da = da
self.hra = hra
self.salary = salary
class emp_pay(employee,payslip):
def __init__(self, name, age, salary,da,hra):
self.name = name
self.age = age
self.salary = salary
self.da = da
self.hra=hra
emp2 = emp_pay('arjun',23,2000,200,200)
print(emp2.age)
print(emp2.salary)
Data Encapsulation
• Data hiding is possible by Encapsulation
• Using Access Specifier we can have data
hiding
Example:
class exam(object):
def __init__(self, a, b, c):
self.p = a # public member
self._pr = b # Protected member
self.__prt = c #Private member
>>> x = exam(11,13,17)
>>> x.p
11
>>> x._pr
13
>>> x._pr= 23
>>> x._pr
23
>>> x.__prt
Traceback (most recent call last): File "<stdin>", line 1, in <module>
AttributeError: ‘exam' object has no attribute '__prt'
Types of methods
• We can classify the methods in the following
3 types:
– Instance methods
• Accessor methods
• Mutator methods generally used for initializing the
private members of a
class
– Class methods
– Static methods
Instance methods types
Accessor methods simply access of read data of the
variables. They do not modify the data in the
variables. Accessor methods are generally written in
the form of getXXXX( ) and hence they are also
called getter methods.
Mutator methods are the methods which not only
read the data but also modify them. They are
written in the form of setXXXX( ) and hence they are
also called setter methods.
Instance Method Example
class Student:
def setName(self,n):
self.__name = n
def setBranch(self,b):
self.__branch = b
def getName(self):
return self.__name
def getBranch(self):
return self.__branch
s=Student()
s.setName('Alex')
s.setBranch('CSE')
print s.getName()
print s.getBranch()
Class Method
class Dog:
@classmethod
def walk(cls):
print('Dog is Walking')
Dog.walk()
Output:
Dog is Walking
Static Method
class Dog:
def __init__(self, breed):
self.breed = breed
@classmethod
def walk(cls):
print('Dog is Walking')
# instance method
def bark(self):
print('{} is barking.'.format(self.breed))
@staticmethod
def add(x, y):
return x + y
Dog.walk()
d = Dog('Tommy')
d.bark()
print(Dog.add(10, 20))
Static Method
Output:
Dog is Walking
Tommy is barking.
30
Method Overriding
• Syntax:
• class Subclass(BaseClass):
– <class body>
• The super( ) method:
• super().init() # call super class constructor
• super().init(arguments) # call super class
constructor and pass arguments
• super().method() # call super class method