You are on page 1of 12

Training On Python

Lecture – 5
OOPS Concepts
OOPS Principles
OOPS are the rules (or) guidelines which are supposed to be satisfied by any programming language in

order to call that programming language as OOPL.

Different oops principles are:-

 Encapsulation

 Polymorphism

 Inheritance

 Abstraction

.
Concept Of Class
Class is a syntax (or) structure used to bind (or) group the related data members along with its related
functionalities.
Syntax:-
class classname:
………………….. # same space indentation called suit/ block/ class
………………….

Within the class we can represent data by using static variables and non static variables.
Example Application Using Class
#This program demonstrate concept of class
class MyClass:
def sayHello(self,name):
print("Hello,",name)
name=input("Enter your name : ")
m=MyClass() #Creation of object
m.sayHello(name) #Calling of class function
Static Variables

 The variables which are declared within the class, outside of all the methods are known as static
variables (or) class variables.

 The data which is common for every object is recommended to represent by using static variables.

 For all the static variables of a class, memory will allocated only once.

 Static variable of one class can access within the same class (or) outside of the class by using class name.
Example Application Using Static Variables

class x:
i=1000 #static variable
def m1(self):
print(x.i)
x1=x() #object creation statement
x1.m1() #1000
print(x.i) #1000
Non Static Variables

 The variables which are declared with self (or) reference variable are known as non-static variables (or)
instance variable.
 Non static variables we can define within the constructors (or) within the method.
 The data which is separate for every object is recommended to represent by using non-static variable.
 For all the non-static variables of a class, memory will be allocated whenever we create object.
 With respect to every object creation statement separate copy of memory will be allocated for non-static
variable.
 Non static variable of one class, we can access within the same class by using ‘self’.
 Non static variables of one class we can access outside the class by using reference variable name.
Example Application Non Static Variables
#Example Application using non-static variable
class Employee:
def setValue(self,empid,empname,salary):
self.empid=empid
self.empname=empname
self.salary=salary
def display(self):
print("Employee Id : ",self.empid)
print("Employee Name : ",self.empname)
print("Employee Salary : ",self.salary)
Example Application Non Static Variables (cont..)
e=Employee() #Creation of object
eid=int(input("Enter Employee Id : "))
ename=input("Enter Employee Name : ")
esalary=int(input("Enter Employee Salary : "))
e.setValue(eid,ename,esalary)
e.display()
Output:-
Enter Employee Id : 1001
Enter Employee Name : Brijesh Mishra
Enter Employee Salary : 50000
Employee Id : 1001
Employee Name : Brijesh Mishra
Employee Salary : 50000
Concept Of Constructor
 It is a special kind of method which executes automatically whenever we create object (i.e., without
calling it).
 Constructors are used to initialize the non-static variables of the class at the time of object with the user
defined value.

Example Application:-
class z:
def __init__(self):
print 'in const of z'
z1=z()
O/P:-
in const of z
Example Of Constructor
# Constructor Demo
class Rectangle:
def __init__(self,l,b):
self.l=l
self.b=b
def rectArea(self):
return self.l*self.b
def rectPeri(self):
return 2*(self.l+self.b)
Example Of Constructor (cont..)
x=int(input("Enter the length : "))
y=int(input("Enter the breadth : "))
r=Rectangle(x,y)
print("Area of rectangle:",r.rectArea())
print("Perimeter of rectangle:",r.rectPeri())

O/P:-
Enter the length : 10
Enter the breadth : 5
Area of rectangle: 50
Perimeter of rectangle: 30

You might also like