OOPS FEATURES
Class : A class is a blue print from which individual objects are created. A class is a template
which describes the kinds of state and behavior that objects of its type support. Each object of a
given class contains the state and behavior defined by the class Class is a logical entity. Class
consists of variables and methods. The sate of the object is mapped with variables of the class
and behavior of the object is mapped with methods of a class.
Object: Object is a basic run time entity . object is an instance of a class. Object is a physical
entity.
Data abstraction: hiding internal implementation details and providing necessary information is
the concept of abstraction.
The main advantage of abstraction is we can provide security .
By using interfaces and abstract classes we can achieve data abstraction in java
Data Encapsulation: The process of binding the data and its associated methods together into a
single unit is nothing but encapsulation.
method
Data method
Inheritance: Inheritance is the process by which objects of one class acquires some or all the
properties and behavior of objects of another class . We can create new classes that are built
upon existing classes. Inheritance represents IS-A relationship. The use of inheritance is code
reusability.
polymorphism: polymorphism means the ability to take more than one form. Defining more
than one method with the same name with different parameters and different types
Message Passing: messages are valid signals to which an object will respond. They are the
labels for the method. Same method may be acted differently by different objects. An action is
initiated in objected oriented programming by transmission of a message to an agent(object)
responsible for that action. The message encodes the request for an action and is accompanied
by an additional piece of information (arguments) , needed to carry out the request
Example: a1 . deposit ( amount )
Receiver selector argument
There are three identifiable parts to any message passing expression
1). The receiver (the object to which the message is being sent )
2). The message selection (the text that indicates the particular message being sent)
3). The arguments ( providing inputs to the message)
CLASSES AND OBJECTS
An object is any real world entity that has state and behavior. Every object has two
characteristics
State: represents what does an object have (properties/attributes/fields)
Behavior: represents the functionalities of an object
For example, pen is an object. Its brand, color, price etc. represents the state. We can write,
refill these are the set of activities that the object can perform and can be performed on the
object. These activities represent the behavior of the object.
Objects are the basic building blocks of object oriented programing. identifying the state and
behavior for real world objects is a great way to begin thinking in terms of object oriented
programming
A class is a blue print from which individual objects are created. It is a template that describes
the kinds of state and behavior that objects of its type support.
Defining a class
class <classname> :
suite
Creating an employee class
#creating an Employee class
class Employee:
pass
#object creation
e1=Employee()
e2=Employee()
#print unique id of the objects
print(id(e1))
print(id(e2))
'''
instance variables contain data that is specific to each instance
'''
''' manually create instance variables for each employee'''
e1.eno=111
e1.ename='Alan’
e1.esal=30000
e2.eno=222
e2.ename='Joe’
e2.esal=40000
‘’’accessing instance variables using object reference ‘’’
print(e1.eno)
print(e1.ename)
print(e1.esal)
print(e2.eno)
print(e2.ename)
print(e2.esal)
Instance variables
The values of the variables are specific to the individual objects.
Employee object
eno=111
ename = ‘Alan’
e1
esal=30000
2213240537440
2213240537440
Employee object
eno=222
ename=’Joe’
e2
esal=40000
2213240540896
2213240540896
Constructors and instance methods
A constructor is a block of code that is executed at the time of object creation to create the
instance variables of an object
Default constructor: In a Python class if programmer does not define any constructor then
default constructor implicitly embedded by python during program compilation, which is
called as a default constructor. Default constructor is always a zero argument constructor.
def __init__( self ) :
pass
Parameterized constructor: Parameterized constructor is one which takes parameters .
Instance method: inside the method implementation if we use at least one instance variable
then that method is called as instance method. Instance method is invoked by an object
reference variable followed by the dot operator followed by method name. Instance method is
dependent on object. Instance method performs operations on individual objects of a class.
Instance method uses instance variables of that object.
self variable
self is a reference variable refers to the current object; the object on which a method or a
constructor is being called. With in an instance method or a constructor self refers to the
current object.
class Employee:
#constructor
def __init__(self,eno,ename,esal):
self.eno=eno
self.ename=ename
self.esal=esal
#instance method
def showDetails(self):
print(‘Employee ID:’,self.eno)
print(‘Name:,self.ename)
print(‘Salary:’,self.esal)
e1=Employee(111,'Alan',30000)
print(id(e1))
e2=Employee(222,'Joe',40000)
print(id(e2))
print(e1.showDetails())
print(e2.showDetails())
CLASS VARIABLES AND CLASS METHODS
class variables: class variables are shared by all the objects of a class. Memory is allocated for
class variables only once when the class is loaded into the memory.
Class methods: if the method implementation uses only class variables and does not use any
of the instance variables then that method is no way related to a particular object . Those
methods are called as class methods. In Python class methods can be defined using
@classmethod annotation . every class method takes first parameter cls which is reference to
the object that holds the information about the current class .
#banking transactions
class Account:
minbal=200 #class variable
def __init__(self,cno,cname): #parameterized constructor
self.cno=cno
self.cname=cname
self.currbal=Account.minbal
def display(self): #instance method
print('Customer Id:',self.cno)
print('Name:',self.cname)
print('CurrentBalance:Rs',self.getBalance())
def getBalance(self):
return self.currbal
@classmethod
def getMinimumBalance(cls): #class method
return cls.minbal
def deposit(self,amount):
self.currbal=self.currbal+amount
print('Amount Rs.',amount,'is credited to your account')
def withdraw(self,amount):
chkbal=self.currbal-amount
if (chkbal < Account.getMinimumBalance()):
print('Insufficient Funds')
else:
self.currbal=self.currbal-amount
print(amount,'is debited from your account')
if __name__=='__main__':
a1=Account(1002,'Akash')
a1.display()
a1.deposit(500)
print('current balance is Rs.',a1.getBalance())
a1.withdraw(250)
print('current balance is Rs.',a1.getBalance())
OUTPUT
Customer Id: 1002
Name: Akash
CurrentBalance:Rs 200
Amount Rs. 500 is credited to your account
current balance is Rs. 700
250 is debited from your account
current balance is Rs. 450
STATIC METHOD
Static method : if the method implementation does not use any of the class variables and also
+ does not use any of the instance variables then that method should be declared as static
method. These methods are general purpose utility methods. Any way the method
implementation may use any number of local variables . static methods can be defined using
@staticmethod annotation
#calculating floor area and fourwalls area of a room
class Room:
def __init__(self,length,breadth,height):
self.length=length
self.breadth=breadth
self.height=height
def display(self):
print('length=',self.length)
print('breadth=',self.breadth)
print('height=',self.height)
def floorArea(self):
fa=self.length*self.breadth
return fa
def fourWallsArea(self):
fwa=2*self.height*(self.length+self.breadth)
return fwa
@staticmethod
def convert(ar):
smt=0.0928*ar
return smt
if __name__=='__main__':
r1=Room(1.2, 3.4, 5.6)
r1.display()
fa=r1.floorArea()
print('Floor Area=',r1.floorArea(),'sft')
print('Floor Area=',Room.convert(fa),'smt')
OUTPUT
length= 1.2
breadth= 3.4
height= 5.6
Floor Area= 4.08 sft
Floor Area= 0.37862399999999996 smt