PPS Unit - 5 Notes
PPS Unit - 5 Notes
1
Click Here Join Official Telegram channel
Advantages:
Monolithic programming language is used only for very small and simple
applications.
Monolithic programming language is used only for applications where
reusability is not a major concern.
Disadvantages:
Monolithic programs have just one program module as monolithic
programming languages do not support the concept of subroutines.
As it is containing just one program module, all the actions required
completing particular task are embedded within same application itself. This
makes the size of program large.
It is difficult to debug and maintain monolithic programs.
2
Click Here Join Official Telegram channel
Advantages:
This paradigm is used to write just correct programs.
Programs using this paradigm are easier to write as compared to
monolithic programming.
Disadvantages:
Writing programs is complex.
No concept of reusability.
It requires more time and effort to write programs.
Programs are difficult to maintain.
Global data is shared and therefore may get altered (mistakenly).
3
Click Here Join Official Telegram channel
Advantages:
Structured programming is used to write correct programs that are easy to
understand and change.
Structured programming allows users to look at the big picture first and
then focus on details later by using modules.
With modules, many programmers can work on a single, large program,
with each working on a different module.
A structured program takes less time to be written than other programs.
Also modules or procedures written for one program can be reused in
other programs as well.
Each module performs a specific task.
Each module has its own local data.
A structured program is easy to change as well as understand because
every procedure has meaningful names and has clear documentation to
identify the task performed by it.
A structured program is easy to debug because each procedure is
specialized to perform just one task and every procedure can be checked
individually for the presence of any error.
Structured programming gives more emphasis on code and the least
importance is given to the data.
4
Click Here Join Official Telegram channel
Disadvantages:
Structured programming is not data-centered.
Global data is shared and may get modified.
In structured programming, main focus is on functions.
5
Click Here Join Official Telegram channel
Advantages:
OOP is used for simulating real world problems on computers because the
real world is made up of objects.
Programs written using OOP are data centered.
Programs are divided in terms of objects and not procedures.
Functions that operate on data are tied together with the data.
Data is hidden and not accessible by external functions.
New data and functions can be easily added as and when required.
It follows a bottom-up approach for problem solving.
Disadvantages:
o It requires more data protection.
o Inability to work with existing systems.
o Larger program size.
o Not suitable for all types of problems-for smaller problems it is in general not
suitable.
6
Click Here Join Official Telegram channel
3. It simulates the real world entity. So It doesn't simulate the real world. It
real-world problems can be easily works on step by step instructions
solved through oops. divided into small parts called
functions.
1. Classes
A class is user-defined data type used to describe something in the world, such
as occurrences, things, external entities, and so on.
A class describes template or blueprint that describes the structure and behavior
of a set of similar objects.
Once we have definition for a class, a specific instance of that class can be easily
created.
For eg. Consider a class student. A student has attributes such a roll_no, name,
course and aggregate. The operations that can be performed on its data may
include ‘getdata’, ‘setdata’,’editdata’ and so on.
A class is collection of objects.
7
Click Here Join Official Telegram channel
2. Objects
Object is basic unit of object-oriented programming.
Object is basic run-time entity in an object-oriented system.
Anything having its own properties can be considered as an object.
For example flower is an object having properties such as name, fragrance, etc.
An object is a collection of data members and associated member function also
known as methods shown in figure1 below:
Object Name
Attribute 1
Attribute 2
.........
Attribute N
Function 1
Function 2
.........
Function N
Fig1. Representation of an Object
8
Click Here Join Official Telegram channel
Figure2. Objects sending a message
Message Passing
Two objects communicate with each other though messages.
An object asks another object to involve one of its methods by sending it a
message.
In figure2 sender object is sending message to the receiver object to get details
of a student. i.e. the sender is passing some specific information to the receiver
so that the receiver can send the correct and precise information to the sender.
The data that is transferred with the message is called parameters. Here, roll_no
1 is the parameter.
The messages that are sent to other objects consist of three aspects-the receiver
object, the name of the method that the receiver should invoke, and the
parameters that must be used with the method.
9
Click Here Join Official Telegram channel
Figure3: Child, derived or subclass
Student
Undergraduate Postgraduate
Student Student
5. Polymorphism
Polymorphism refers to having several different forms.
While inheritance is related to classes and their hierarchy, polymorphism on
other hand, is related to methods.
Polymorphism is a concept that enables the programmers to assign a different
meaning or usage to a method in different context.
Polymorphism exist when a number of subclasses is defined which have
methods of same name.
Polymorphism can also be applied to operators.
11
Click Here Join Official Telegram channel
7. Reusability
Reusability means developing codes that can be reused either in the same
program or in the different programs.
Reusability is attained through inheritance, containership and polymorphism.
8. Delegation
To provide maximum flexibility to programmers and to allow them to generate a
reusable code, object oriented languages also support delegation.
In composition, an object can be composed of other objects and thus, the object
exhibits a ‘has-a’ relationship.
In delegation, more than one object is involved in handling a request. The object
that receives the request for a service, delegates it to another object called its
delegate.
Delegation means that one object is dependent on another object to provide
functionalities.
The property of delegation emphasizes on the ideology than a complex object is
made of several simpler objects.
For example, our body is made up of brain, heart, hands, eyes, ears, etc. The
functioning of the whole body as a system rests on the correct functioning of the
parts it is composed of.
Delegation is different from inheritance in the way that two classes that
participates in inheritance share ‘is-a’ relationship; however, in delegation, they
have a ‘has-a’ relationship.
9. Data Abstraction
Data Abstraction refers to the process by which data and functions are defined
in such a way that only essential details are revealed and the implementation
details are hidden.
The main focus of data abstraction is to separate the interface and
implementation of a program.
12
Click Here Join Official Telegram channel
For example, as user of television sets, we can switch it on or off, change the
channel, set the volume and add external devices such as speakers and CD or
DVD players without knowing the details how its functionality has been
implemented. Therefore the implementation is completely hidden from external
world.
13
Click Here Join Official Telegram channel
In fact, in Python, everything is an object o an instance of some class.
For example, all integer variables that we define in our program are actually
instances of class int. We can find out the type of any object using the type()
function.
Defining Classes
Python has very simple syntax of defining a class. Syntax is given below:
class class_name:
<statement-1>
<statement-2>
.
.
.
<statement-N>
It starts with a keyword class followed by the class_name and colon (:)
The statement in the definition can be any of these—
o Sequential instructions,
o Decision control statements
o Loop statements and
o Function definition
Variables defined in the class are called class variables and functions defined
inside a class are called class method. Class variables and class methods are
together known as class members.
The class members can be accessed through class objects.
Class definitions can appear anywhere in a program, but they usually written
near the beginning of the program, after import statement.
When a class definition is entered, a new namespace is created, and used as the
local scope.
14
Click Here Join Official Telegram channel
Creating Objects
Once a class is defined, the next job is to create an object (or instance) of that
class. The object can then access class variables and class methods using dot
operator (.)
The syntax to create an object is given as:
Object_name=class_name()
Creating an object or instance of a class is known as class instantiation. Class
instantiation uses function notation.
The syntax for accessing a class member through the class object is:
Object_name.class_member_name
Program to access class variable using class object
class ABC:
var=10
obj=ABC() #Object obj is created of class ABC
print([Link])
OUTPUT
10
In the above program, we have defined a class ABC which has var having a
value of 10. The object of the class is created and used to access the class
variable using dot operator.
15
Click Here Join Official Telegram channel
Encapsulation (Data hiding)
Data encapsulation, also called data hiding, is the technique of packing data and
functions into a single component (class) to hide implementation details of a
class from users.
Users are allowed to execute only a restricted set of operations (class methods)
on the data members of the class.
Therefore encapsulation organizes the data and methods into a structure that
prevents data access by any function (or method) that is not specified in the
class. This ensures the integrity of the data contained in the object.
Encapsulation defines three access levels for data variables and member
functions of the class. These access levels specify the access rights, explained as
follows:
o Any data of function with access level as p ublic can be accessed by any
function belonging to any class. This is lowest level of data protection.
o Any data of function with access level as protected can be accessed only
by that class or by any class that is inherited from it.
o Any data of function with access level as private can be accessed only
by the class in which it is declared. This is highest level of data
protection.
16
Click Here Join Official Telegram channel
.
Statement n
Class method must have the first argument named as self. Moreover we do not pass
any value for this parameter.
When we call the method, python provide its value automatically. Self-argument
refers to the object itself.
This means that even that method that takes no arguments, it should be defined to
accept the self.
self is just a parameter in function and user can use any parameter name in place of
it. But it is advisable to use self because it increase the readability of code.
Example:
Output:
Roll no of Student is 10
17
Click Here Join Official Telegram channel
It is also called as an initializer method or constructor in object oriented
programming.
It is useful to initialize the variables of the class objects.
First parameter of this method is called as self, means instance/object of the class.
This method is prefixed as well as suffixed with double underscore symbol
Syntax:
Class Class_name:
def init (self [,argument list]):
Statement 1
Statement 2
.
.
Statement n
Class variables are created and assigned values within declaration itself.
You can also access class variables with an object created within a class
Example 2:
obj= student()
Object Variable:
Object variable or instance variable have different value for each new instance.
If a class has N instances/Objects, then there will be a N separate copies of the object
variable as each object/instances have its own object variable.
Object variables are not shared between other objects.
19
Click Here Join Official Telegram channel
A change made to the object variable by one object, will not be replicated in other
objects.
Example1:
class student: #class Defination
serial_No=0 # Class Variable
def init (self, marks): # Class Method (Constructor)
student.serial_No+=1
[Link]=marks
print(("Serial number is" , self.serial_No)
print ("Marks of a Student is",[Link])
Example 2:
class student: #class Defination
serial_No=0 # Class Variable
def display(self, marks): # Class Method
student.serial_No+=1
[Link]=marks
print(("Serial number is", self.serial_No)
print ("Marks of a Student is",[Link])
20
Click Here Join Official Telegram channel
Q9. Differentiate between class variables and instance variables.
Ans: We have seen that a Class can have variables defined in it. Basically these variables
are of two types:
a) Class Variables
b) Instance Variables
As the name suggests the class variables are owned by the class and Instance (Object)
variables are owned by each object.
Important points are as follows:
If a class has n objects, then there will be n separate copies of the object variable as
each object will have its own object variable.
The object variable is not shared between objects.
A change made to the object variable by one object will not be reflected in other
objects.
If the class has one class variable, then there will be one copy only for that variable.
All the objects of that class will share the class variable.
Since there exists only one copy of class variable, any change made to the class
variable will be reflected in all other objects.
Program to differentiate between object and class variables
class Car:
wheels = 4 #Class Variable
def init (self, comp):
[Link] = comp # Object/Instance Variable
print("The company(Object Var) : ", [Link])
print("The Wheels(Class var) : ", [Link])
obj1 = Car("Mercedez")
obj2 = Car("BMW")
Output:
The company(Object Var) : Mercedez
The Wheels(Class var) : 4
The company(Object Var) : BMW
The Wheels(Class var) : 4
21
Click Here Join Official Telegram channel
In the above example, following points should be noted:
The class Variable wheels is having default value 4, which is same for all types of
Cars. Such variables are declared as class variable, which is common to all.
Whereas the Company of car is depends on the particular car and it may varies for
different cars. Such variables are declared as a Instance Variables.
The Object or Instance variables are declared/ initialized inside init () or member
function of a class
The class variable are declared / Initialized outside of any function in class.
Output:
The object value is = 10
The object value is = 20
Object with value 10 is going out of scope
Object with value 20 is going out of scope
23
Click Here Join Official Telegram channel
def display(self):
print("From class method var1=", self.var1) # var1 is accessible within class
print("From class method var2=", self. var2) # var2 is accessible within class
obj=ABC(10,20)
[Link]()
print("From main module, var1 =", obj.var1) # var1 is public, accessible outside class
print("From main module, var2 =", obj. var2) #Will give Error as var2 is private
Output:
From class method var1= 10
From class method var2= 20
From main module, var1 = 10
Error Message displayed
As a good programming habit, you should never try to access a private variable from
anywhere outside the class.
But if for some reason, you want to access the private variables outside the class, use
following Syntax:
objectName._className privatevariable
So to remove error from above code, you shall write last statement as
print("From main module, var2 =", obj._ABC var2)
Private Methods:
We know that private attributes should not be accessed from anywhere outside the
class.
Similarly, you can have private methods in your class. Which are accessible only
within the class.
class ABC:
def init (self, var1, var2):
self.var1 = var1 # var1 is public variable
self. var2= var2 # var2 is private variable
24
Click Here Join Official Telegram channel
def update(self, var1): #Public Method
self.var1 = var1 # var1 is accessible within class
self. display() #Accessing private method within the class
Output:
From class method var1= 30
From class method var2= 20
From class method var1= 30
From class method var2= 20
class ABC():
def method1(self):
print("This is from method1")
def method2(self):
print("Executed method 2")
self.method1() #method1() called in method2
25
Click Here Join Official Telegram channel
obj1=ABC()
obj1.method2()
Output:
From method 2
This is from method1
Note: In the above program we have called method2 only. Method 2 consist a call for
method 1. So method 1 will also get executed.
Example:
class Rectangle:
def init (self,length,breadth):
[Link]=length
[Link]=breadth
def area(self):
return [Link]*[Link]
26
Click Here Join Official Telegram channel
@classmethod
def Square(cls,side):
return cls(side,side)
S=[Link](10)
print("Area=",[Link]())
Output:
Area= 100
27
Click Here Join Official Telegram channel