• Programming Paradigms-monolithic, procedural, structured and object oriented ,
• Features of Object oriented programming:
• classes, objects, methods and message passing, inheritance, polymorphism,
containership, reusability, delegation, data abstraction and encapsulation
• Classes and Objects:
• classes and objects, class method and self object,
• class variables and object variables,
• public and private members, class methods.
Zeal College of Engineering and Research
UNIT: V Object Oriented Programming Lectures:
08 Hrs
 Programming paradigm is an approach to solve problem using
some programming language.
 There are lots for programming language that are known but all
of them need to follow some strategy when they are
implemented and this methodology/strategy is paradigms.
 Various programming paradigms are
1. Monolithic Programming
2. Procedural Programming
3. Structural Programming
4. Object Oriented Programming
Programming Paradigm:
1. Monolithic Programming:
 The Program which contains a single function for the large
program is called monolithic program.
 A program is not divided into parts
 When program size increases it also increase difficulty.
 The program is sequential code and variable used is global data.
 goto statement is used for specific jump or flow control
statement.
Advantages:
 Easy to implement programs for small applications.
Disadvantages:
 If the program size is large then it is difficult to check error.
 Due to single function, the program is difficult to maintain.
 Code cannot be reused as it is written for specific problem.
2. Procedural Programming
 Procedural programming is a programming paradigm that uses
a linear or top-down approach.
 It relies on functions (procedures or subroutines) to perform
computations.
 Procedural programming is also known as imperative
programming.
 Large programs are divided into smaller programs known as
functions.
 Function access the global data.
 Top down approach
Advantages:
Simple and easy to write.
These language have low memory utilization.
Disadvantages:
Parallel programming is not possible.
Difficult to maintain programs.
Less productive.
 Also known as modular programming.
 The structured program mainly consist of
1. Selection Statements (if, if…else, if….elif…else statements)
2. Sequence Statements
3. Iteration Statements (loops- for, while)
 The program consist of structured and separate modules.
 Example: C, Pascal
3. Structural Programming:
Advantages:
 Program is easy to implement and maintain.
 Programs are problem based not the machine based.
Disadvantages:
 Conversion of program to machine code takes time.
Savitribai Phule Pune University -2019-20
4. Object Oriented Programming:
 The program is written as a collection of classes and object
which are meant for communication.
 The smallest and basic entity is object and all kind of
computation is performed on the objects only.
 Data and function are grouped into one entity called class.
 Programs are divided into classes and member functions.
Savitribai Phule Pune University -2019-20
Advantages:
 Importance is given to data.
 Bottom up Approach
 Data is hidden and can not be accessed by external functions.
Disadvantages:
 Complex to implement.
Savitribai Phule Pune University -2019-20
Difference between procedure oriented language and object oriented language.
Savitribai Phule Pune University -2019-20
Sr
No.
Procedural Oriented
Language
Object oriented language
1
Importance is given to
algorithm
Importance is given to data
2 Top down appraoch Bottom Up Approach
3
Programs are divided into
functions
Programs are divided into objects
4 Data is not hidden
Data is hidden and can not be
accessed by external functions
5 no access specifier
private, public, protected access
specifier is used.
6 less secure more secure
7 Example: C, Pascal Example: C++, Java ,Python
Programming languages and the programming paradigms
they support.
C++ - Monolithic, Structured-oriented, Procedural-oriented and, Object-
oriented programming paradigm
Python - Object Oriented, Procedure, and Functional programming
paradigms
Java - Procedural and object oriented paradigm
JavaScript - functional, object-oriented, procedural, and prototypal
programming
C# - imperative, declarative, functional, generic, object-oriented (class-
based), and component-oriented
PHP - Procedural, Object-Oriented, and functional paradigm
Ruby - procedural, object-oriented, and functional programming
Visual Basic - object-oriented
Features of Object oriented programming:
1. Class:
 class is a collection of objects.
 It is a logical entity that has data members and methods
(member functions).
 Once class has been defined, we can create any number of
objects belonging to that class.
 Ex: Mango, Apple and Orange are the members of Fruit class.
Syntax:
class classname:
Ex:
class Fruit:
Savitribai Phule Pune University -2019-20
Syntax:
class classname:
statement 1 #Data members and Member functions
statement 2
statement 3
Ex:
class Student:
rollno = 101 # Data members
name = "Priya"
def fun(self):
print('Hello') # Member Functions
Savitribai Phule Pune University -2019-20
Class Method and self object:
 The class methods are the function defined inside the class.
 The class method defines the first argument as self. This is always
defined as the first argument.
 If there is no argument in class method then only self is the argument for
that method.
 The self argument refers to object itself.
Savitribai Phule Pune University -2019-20
2. Object:
 Object is an instance of class. The object is an entity that has state and
behavior.
 It may be any real-world object like the person, place, bank account or
any item etc.
 Everything in Python is an object.
Syntax:
objectname = classname( )
Ex:
obj = Fruit( )
Savitribai Phule Pune University -2019-20
Savitribai Phule Pune University -2019-20
Sr
No.
Class Object
1 Class is collection of
objects. Class is collection
of data members and
member functions.
An object is instance of a class.
2 Class is a logical entity. Object is a physical entity.
3 Class is declared once. Object is created many times.
4 Class doesn't allocated
memory when it is
created.
Object allocates memory when it
is created.
5
Ex: Class: Fruit
Ex: Object: Apple, Banana,
Mango, Guava
3. Method:
 The method is a function that is associated with an object.
 There must be a special first argument self in all of method definitions.
 Any object type can have methods.
 There is usually a special method called __init__ ( )in most classes.
Savitribai Phule Pune University -2019-20
Example
1. Write a python program to find area of rectangle using class.
class Rect:
def __init__(self,length,breadth):
self.length=length
self.breadth=breadth
def Area(self):
print("Area of rectangle is: ",r1.length*r1.breadth)
r1 = Rect(160,120)
r1.Area()
OUTPUT:
Area of rectangle is: 19200
4. Inheritance:
 Inheritance is the process by which object of one class acquires
properties object of another class.
 reusability concept is used.
 Sub Class: The class that inherits properties from another class is called
Sub class or Derived Class.
 Super Class/ Base class: The class whose properties are inherited by
sub class is called Base Class or Super class.
Example: Dog, Cat, Cow can be Derived Class of Animal Base Class.
Savitribai Phule Pune University -2019-20
Single Level Inheritance
class E:
def U_test_Marks(self):
print("Unit test marks submitted successfully")
def C_test_Marks(self):
print("Class test marks submitted successfully")
f1 = E()
f1.U_test_Marks()
f1.C_test_Marks()
class F(E):
pass
f2 = F()
f2.U_test_Marks()
5. Reusability:
 The child class inherits some behaviour of parent class.
 For child class, it is not necessary to write code for inherited behavior,
i.e. child class directly used the methods written in parent class.
 For instance, the class Student inherits Person class. Hence the method
such as getName( ), getAddress( ) of parent class Person can be
directly used by the Student class. The Student class can have its own
additional method such as getMarks( ).
 Thus methods can be written once and can be reused.
Savitribai Phule Pune University -2019-20
6. Polymorphism:
 Ability to perform more than one forms.
 An operation may exhibit different behaviors in different instances.
 The behavior depends upon the types of data used in the operation.
Savitribai Phule Pune University -2019-20
1. Operator Overloading
2. Method Overriding
Method Overriding
class E:
def name1(self,name):
print(name)
f1 = E()
f1.name1("punam")
class F:
def name1 (self,name):
print(name)
f2 = F()
f1. name1("punamsawale")
7. Containership:
 one class contain the object of another class.
 This type of relationship between classes is known as containership or
has a relationship.
 The class which contains the object and members of another class in this
kind of relationship is called a container class.
Ex: 1) computer system has a hard disk
2) car has an Engine, chassis, steering wheels.
Savitribai Phule Pune University -2019-20
8. Delegation:
 This is feature of object oriented programming by which one class
depends upon the other class.
 It is represented by has a relationship.
 In composition – The child cannot exist if parent class is not present i.e.
child class depends on parent class. It is strong relationship.
 In delegation there is dependency of one class on another but even if
the parent class is not present the dependent class exists.
 Ex: A teacher may belong to multiple departments. So, Teacher is a part
of multiple departments. But lets say if we “delete” a Department, the
Teacher will still be there. This is delegation.
 A school can contain multiple classrooms. Now if we “delete” the
school, the classrooms will automatically be deleted. This is
composition.
Savitribai Phule Pune University -2019-20
9. Data Abstraction and Encapsulation:
1. Data abstraction
 Data abstraction means representing only essential features by hiding all
the implementation details.
 Class is entity used for data abstraction.
 The methods are defined in class. From main function we can access
these functionalities using objects.
2. Encapsulation
 Encapsulation means binding of data and methods in single
entity called class.
 The data inside the class is accessible by the function in same class.
 Public and Private access specifier is used for data protection
 It is normally not accessible from outside of the component.
Savitribai Phule Pune University -2019-20
Savitribai Phule Pune University -2019-20
Savitribai Phule Pune University -2019-20
Sr
No.
Data abstraction Data encapsulation
1
Representing only essential
features by hiding all the
implementation details.
binding of data and methods in
single entity called class.
2
It is used in software design
phase.
It is used in software
implementation phase.
3
abstraction using Abstract
Class.
encapsulation using Access
Modifiers (Public, Protected &
Private.)
4
Depends on object data
type.
independent on object data type.
5
Focus mainly on what
should be done.
Focus primarily on how it should
be done.
Class Variables and object variables
1. Class variable
 The variables at the class level are called class variables.
 The class variables are shared by all the instances of the class i.e. same
value is used for every instance or object
 There is single copy of class variable which is shared among all the
objects, hence changes made by an object in a single copy of class
variable is reflected in all other objects.
2. Object Variable
 The variable owned by each object is called object variable.
 For each object or instance of class, the instance variables are different.
 Object variables are defined in methods.
 The changes made in one object variable will not be reflected for other
object variable.
Savitribai Phule Pune University -2019-20
class PPS:
Attendance = 0 #class variable
def __init__(self, var):
PPS.Attendance += 1
self.var = var #object variable
print("Lecture No: ", PPS.Attendance)
print("Total student present for lecture 1 : ", var)
obj1 = PPS(50)
obj2 = PPS(52)
obj3 = PPS(55)
Savitribai Phule Pune University -2019-20
Output:
Lecture No: 1
Total student present for lecture 1 : 50
Lecture No: 2
Total student present for lecture 1 : 52
Lecture No: 3
Total student present for lecture 1 : 55
Savitribai Phule Pune University -2019-20
Public and private members:
 Public variables are those variables that are defined in the class and can be
accessed from within class and from outside the class using dot operator.
 All members in a python class are public by default.
 Private variables are those variables that are defined in the class but can
be accessible by the methods of that class only.
 The private variables are used with double underscore( __ ) prefix.
 Protected variables :The members of a class that are declared protected
are only accessible to a class derived from it.
 Data members of a class are declared protected by adding a single
underscore (_) symbol before the data member of that class.
Savitribai Phule Pune University -2019-20
class ABC:
def __init__(self, var1, var2):
self.var1 = var1
self.__var2 = var2
def display(self):
print("From class method, variable 1 is: ", self.var1)
print("From class method, variable 2 is: ", self.__var2)
obj = ABC(10,20)
obj.display()
print("From class method, variable 1 is: ", obj.var1)
print("From class method, variable 2 is: ", obj.__var2)
Output:
From class method, variable 1 is: 10
From class method, variable 2 is: 20
From class method, variable 1 is: 10
Traceback (most recent call last)
---> 13 print("From class method, variable 2 is: ", obj.__var2)
AttributeError: 'ABC' object has no attribute '__var2'
Savitribai Phule Pune University -2019-20
Savitribai Phule Pune University -2019-20
To remove above error, to access private variable use
following syntax:
objectname._classname__privatevariable
Ex:
print("From class method, variable 2 is: ",obj._ABC__var2)
Savitribai Phule Pune University -2019-20
To access private method, use following syntax:
objectname._classname__privatemethodname
Ex:
class ABC:
def __init__(self, var):
self.__var = var
def __display(self):
print("From class method,variable is:",
self.__var)
obj = ABC(10)
obj._ABC__display()
Output:
From class method, variable is: 10
Savitribai Phule Pune University -2019-20
Write a program in python to Create class EMPLOYEE for storing details (Name,
Designation, gender, Date of Joining and Salary). Define function members to display all
EMPLOYEE details.
class Employee:
name = []
designation = []
gender = []
doj = []
salary = []
def __init__(self):
for i in range(3):
self.name.append(input("Enter the name:"))
self.designation.append(input("Enter the Designation:"))
self.gender.append(input("Enter the gender:"))
self.doj.append(input("Enter the doj:"))
self.salary.append(int(input("Enter the salary:")))
Savitribai Phule Pune University -2019-20
def display(self):
print("-------------Details of Employee-----------------")
print(" NamettDesignationtGenderttDate of
JoiningttSalary ")
print("-------------------------------------------------")
for i in range(3):
print(self.name[i],"tt",self.designation[i],"t",
self.gender[i],"tt",self.doj[i],"tt",self.salary[i] )
e = Employee()
e.display()
Output:
Enter the name:Sakshi
Enter the Designation:Manager
Enter the gender:Male
Enter the doj:10-06-2001
Enter the salary:50000
Savitribai Phule Pune University -2019-20
Enter the name:Rohit
Enter the Designation:Developer
Enter the gender:Male
Enter the doj:10-05-2009
Enter the salary:35000
Enter the name:Riya
Enter the Designation:Tester
Enter the gender:Female
Enter the doj:10-02-2012
Enter the salary:20000
----------------------Details of Employee--------------------------
Name Designation Gender Date of Joining Salary
-------------------------------------------------------------------
Sakshi Manager Male 10-06-2001 50000
Rohit Developer Male 10-05-2009 35000
Riya Tester Female 10-02-2012 20000

Unit-V.pptx

  • 1.
    • Programming Paradigms-monolithic,procedural, structured and object oriented , • Features of Object oriented programming: • classes, objects, methods and message passing, inheritance, polymorphism, containership, reusability, delegation, data abstraction and encapsulation • Classes and Objects: • classes and objects, class method and self object, • class variables and object variables, • public and private members, class methods. Zeal College of Engineering and Research UNIT: V Object Oriented Programming Lectures: 08 Hrs
  • 2.
     Programming paradigmis an approach to solve problem using some programming language.  There are lots for programming language that are known but all of them need to follow some strategy when they are implemented and this methodology/strategy is paradigms.  Various programming paradigms are 1. Monolithic Programming 2. Procedural Programming 3. Structural Programming 4. Object Oriented Programming Programming Paradigm:
  • 3.
    1. Monolithic Programming: The Program which contains a single function for the large program is called monolithic program.  A program is not divided into parts  When program size increases it also increase difficulty.  The program is sequential code and variable used is global data.  goto statement is used for specific jump or flow control statement.
  • 4.
    Advantages:  Easy toimplement programs for small applications. Disadvantages:  If the program size is large then it is difficult to check error.  Due to single function, the program is difficult to maintain.  Code cannot be reused as it is written for specific problem.
  • 5.
    2. Procedural Programming Procedural programming is a programming paradigm that uses a linear or top-down approach.  It relies on functions (procedures or subroutines) to perform computations.  Procedural programming is also known as imperative programming.  Large programs are divided into smaller programs known as functions.  Function access the global data.  Top down approach
  • 6.
    Advantages: Simple and easyto write. These language have low memory utilization. Disadvantages: Parallel programming is not possible. Difficult to maintain programs. Less productive.
  • 7.
     Also knownas modular programming.  The structured program mainly consist of 1. Selection Statements (if, if…else, if….elif…else statements) 2. Sequence Statements 3. Iteration Statements (loops- for, while)  The program consist of structured and separate modules.  Example: C, Pascal 3. Structural Programming:
  • 8.
    Advantages:  Program iseasy to implement and maintain.  Programs are problem based not the machine based. Disadvantages:  Conversion of program to machine code takes time. Savitribai Phule Pune University -2019-20
  • 9.
    4. Object OrientedProgramming:  The program is written as a collection of classes and object which are meant for communication.  The smallest and basic entity is object and all kind of computation is performed on the objects only.  Data and function are grouped into one entity called class.  Programs are divided into classes and member functions. Savitribai Phule Pune University -2019-20
  • 10.
    Advantages:  Importance isgiven to data.  Bottom up Approach  Data is hidden and can not be accessed by external functions. Disadvantages:  Complex to implement. Savitribai Phule Pune University -2019-20
  • 11.
    Difference between procedureoriented language and object oriented language. Savitribai Phule Pune University -2019-20 Sr No. Procedural Oriented Language Object oriented language 1 Importance is given to algorithm Importance is given to data 2 Top down appraoch Bottom Up Approach 3 Programs are divided into functions Programs are divided into objects 4 Data is not hidden Data is hidden and can not be accessed by external functions 5 no access specifier private, public, protected access specifier is used. 6 less secure more secure 7 Example: C, Pascal Example: C++, Java ,Python
  • 12.
    Programming languages andthe programming paradigms they support. C++ - Monolithic, Structured-oriented, Procedural-oriented and, Object- oriented programming paradigm Python - Object Oriented, Procedure, and Functional programming paradigms Java - Procedural and object oriented paradigm JavaScript - functional, object-oriented, procedural, and prototypal programming C# - imperative, declarative, functional, generic, object-oriented (class- based), and component-oriented PHP - Procedural, Object-Oriented, and functional paradigm Ruby - procedural, object-oriented, and functional programming Visual Basic - object-oriented
  • 13.
    Features of Objectoriented programming: 1. Class:  class is a collection of objects.  It is a logical entity that has data members and methods (member functions).  Once class has been defined, we can create any number of objects belonging to that class.  Ex: Mango, Apple and Orange are the members of Fruit class. Syntax: class classname: Ex: class Fruit: Savitribai Phule Pune University -2019-20
  • 14.
    Syntax: class classname: statement 1#Data members and Member functions statement 2 statement 3 Ex: class Student: rollno = 101 # Data members name = "Priya" def fun(self): print('Hello') # Member Functions Savitribai Phule Pune University -2019-20
  • 15.
    Class Method andself object:  The class methods are the function defined inside the class.  The class method defines the first argument as self. This is always defined as the first argument.  If there is no argument in class method then only self is the argument for that method.  The self argument refers to object itself. Savitribai Phule Pune University -2019-20
  • 16.
    2. Object:  Objectis an instance of class. The object is an entity that has state and behavior.  It may be any real-world object like the person, place, bank account or any item etc.  Everything in Python is an object. Syntax: objectname = classname( ) Ex: obj = Fruit( ) Savitribai Phule Pune University -2019-20
  • 17.
    Savitribai Phule PuneUniversity -2019-20 Sr No. Class Object 1 Class is collection of objects. Class is collection of data members and member functions. An object is instance of a class. 2 Class is a logical entity. Object is a physical entity. 3 Class is declared once. Object is created many times. 4 Class doesn't allocated memory when it is created. Object allocates memory when it is created. 5 Ex: Class: Fruit Ex: Object: Apple, Banana, Mango, Guava
  • 18.
    3. Method:  Themethod is a function that is associated with an object.  There must be a special first argument self in all of method definitions.  Any object type can have methods.  There is usually a special method called __init__ ( )in most classes. Savitribai Phule Pune University -2019-20
  • 19.
    Example 1. Write apython program to find area of rectangle using class. class Rect: def __init__(self,length,breadth): self.length=length self.breadth=breadth def Area(self): print("Area of rectangle is: ",r1.length*r1.breadth) r1 = Rect(160,120) r1.Area() OUTPUT: Area of rectangle is: 19200
  • 20.
    4. Inheritance:  Inheritanceis the process by which object of one class acquires properties object of another class.  reusability concept is used.  Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.  Super Class/ Base class: The class whose properties are inherited by sub class is called Base Class or Super class. Example: Dog, Cat, Cow can be Derived Class of Animal Base Class. Savitribai Phule Pune University -2019-20
  • 21.
    Single Level Inheritance classE: def U_test_Marks(self): print("Unit test marks submitted successfully") def C_test_Marks(self): print("Class test marks submitted successfully") f1 = E() f1.U_test_Marks() f1.C_test_Marks() class F(E): pass f2 = F() f2.U_test_Marks()
  • 22.
    5. Reusability:  Thechild class inherits some behaviour of parent class.  For child class, it is not necessary to write code for inherited behavior, i.e. child class directly used the methods written in parent class.  For instance, the class Student inherits Person class. Hence the method such as getName( ), getAddress( ) of parent class Person can be directly used by the Student class. The Student class can have its own additional method such as getMarks( ).  Thus methods can be written once and can be reused. Savitribai Phule Pune University -2019-20
  • 23.
    6. Polymorphism:  Abilityto perform more than one forms.  An operation may exhibit different behaviors in different instances.  The behavior depends upon the types of data used in the operation. Savitribai Phule Pune University -2019-20 1. Operator Overloading 2. Method Overriding
  • 24.
    Method Overriding class E: defname1(self,name): print(name) f1 = E() f1.name1("punam") class F: def name1 (self,name): print(name) f2 = F() f1. name1("punamsawale")
  • 25.
    7. Containership:  oneclass contain the object of another class.  This type of relationship between classes is known as containership or has a relationship.  The class which contains the object and members of another class in this kind of relationship is called a container class. Ex: 1) computer system has a hard disk 2) car has an Engine, chassis, steering wheels. Savitribai Phule Pune University -2019-20
  • 27.
    8. Delegation:  Thisis feature of object oriented programming by which one class depends upon the other class.  It is represented by has a relationship.  In composition – The child cannot exist if parent class is not present i.e. child class depends on parent class. It is strong relationship.  In delegation there is dependency of one class on another but even if the parent class is not present the dependent class exists.  Ex: A teacher may belong to multiple departments. So, Teacher is a part of multiple departments. But lets say if we “delete” a Department, the Teacher will still be there. This is delegation.  A school can contain multiple classrooms. Now if we “delete” the school, the classrooms will automatically be deleted. This is composition. Savitribai Phule Pune University -2019-20
  • 28.
    9. Data Abstractionand Encapsulation: 1. Data abstraction  Data abstraction means representing only essential features by hiding all the implementation details.  Class is entity used for data abstraction.  The methods are defined in class. From main function we can access these functionalities using objects. 2. Encapsulation  Encapsulation means binding of data and methods in single entity called class.  The data inside the class is accessible by the function in same class.  Public and Private access specifier is used for data protection  It is normally not accessible from outside of the component. Savitribai Phule Pune University -2019-20
  • 29.
    Savitribai Phule PuneUniversity -2019-20
  • 30.
    Savitribai Phule PuneUniversity -2019-20 Sr No. Data abstraction Data encapsulation 1 Representing only essential features by hiding all the implementation details. binding of data and methods in single entity called class. 2 It is used in software design phase. It is used in software implementation phase. 3 abstraction using Abstract Class. encapsulation using Access Modifiers (Public, Protected & Private.) 4 Depends on object data type. independent on object data type. 5 Focus mainly on what should be done. Focus primarily on how it should be done.
  • 31.
    Class Variables andobject variables 1. Class variable  The variables at the class level are called class variables.  The class variables are shared by all the instances of the class i.e. same value is used for every instance or object  There is single copy of class variable which is shared among all the objects, hence changes made by an object in a single copy of class variable is reflected in all other objects. 2. Object Variable  The variable owned by each object is called object variable.  For each object or instance of class, the instance variables are different.  Object variables are defined in methods.  The changes made in one object variable will not be reflected for other object variable. Savitribai Phule Pune University -2019-20
  • 32.
    class PPS: Attendance =0 #class variable def __init__(self, var): PPS.Attendance += 1 self.var = var #object variable print("Lecture No: ", PPS.Attendance) print("Total student present for lecture 1 : ", var) obj1 = PPS(50) obj2 = PPS(52) obj3 = PPS(55) Savitribai Phule Pune University -2019-20
  • 33.
    Output: Lecture No: 1 Totalstudent present for lecture 1 : 50 Lecture No: 2 Total student present for lecture 1 : 52 Lecture No: 3 Total student present for lecture 1 : 55 Savitribai Phule Pune University -2019-20
  • 34.
    Public and privatemembers:  Public variables are those variables that are defined in the class and can be accessed from within class and from outside the class using dot operator.  All members in a python class are public by default.  Private variables are those variables that are defined in the class but can be accessible by the methods of that class only.  The private variables are used with double underscore( __ ) prefix.  Protected variables :The members of a class that are declared protected are only accessible to a class derived from it.  Data members of a class are declared protected by adding a single underscore (_) symbol before the data member of that class. Savitribai Phule Pune University -2019-20
  • 35.
    class ABC: def __init__(self,var1, var2): self.var1 = var1 self.__var2 = var2 def display(self): print("From class method, variable 1 is: ", self.var1) print("From class method, variable 2 is: ", self.__var2) obj = ABC(10,20) obj.display() print("From class method, variable 1 is: ", obj.var1) print("From class method, variable 2 is: ", obj.__var2) Output: From class method, variable 1 is: 10 From class method, variable 2 is: 20 From class method, variable 1 is: 10 Traceback (most recent call last) ---> 13 print("From class method, variable 2 is: ", obj.__var2) AttributeError: 'ABC' object has no attribute '__var2' Savitribai Phule Pune University -2019-20
  • 36.
    Savitribai Phule PuneUniversity -2019-20 To remove above error, to access private variable use following syntax: objectname._classname__privatevariable Ex: print("From class method, variable 2 is: ",obj._ABC__var2)
  • 37.
    Savitribai Phule PuneUniversity -2019-20 To access private method, use following syntax: objectname._classname__privatemethodname Ex: class ABC: def __init__(self, var): self.__var = var def __display(self): print("From class method,variable is:", self.__var) obj = ABC(10) obj._ABC__display() Output: From class method, variable is: 10
  • 38.
    Savitribai Phule PuneUniversity -2019-20 Write a program in python to Create class EMPLOYEE for storing details (Name, Designation, gender, Date of Joining and Salary). Define function members to display all EMPLOYEE details. class Employee: name = [] designation = [] gender = [] doj = [] salary = [] def __init__(self): for i in range(3): self.name.append(input("Enter the name:")) self.designation.append(input("Enter the Designation:")) self.gender.append(input("Enter the gender:")) self.doj.append(input("Enter the doj:")) self.salary.append(int(input("Enter the salary:")))
  • 39.
    Savitribai Phule PuneUniversity -2019-20 def display(self): print("-------------Details of Employee-----------------") print(" NamettDesignationtGenderttDate of JoiningttSalary ") print("-------------------------------------------------") for i in range(3): print(self.name[i],"tt",self.designation[i],"t", self.gender[i],"tt",self.doj[i],"tt",self.salary[i] ) e = Employee() e.display() Output: Enter the name:Sakshi Enter the Designation:Manager Enter the gender:Male Enter the doj:10-06-2001 Enter the salary:50000
  • 40.
    Savitribai Phule PuneUniversity -2019-20 Enter the name:Rohit Enter the Designation:Developer Enter the gender:Male Enter the doj:10-05-2009 Enter the salary:35000 Enter the name:Riya Enter the Designation:Tester Enter the gender:Female Enter the doj:10-02-2012 Enter the salary:20000 ----------------------Details of Employee-------------------------- Name Designation Gender Date of Joining Salary ------------------------------------------------------------------- Sakshi Manager Male 10-06-2001 50000 Rohit Developer Male 10-05-2009 35000 Riya Tester Female 10-02-2012 20000