Pps Unit 5
Pps Unit 5
Pps Unit 5
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
PPS Unit-V DIT,Pimpri
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
PPS Unit-V DIT,Pimpri
9
PPS Unit-V DIT,Pimpri
Student
Undergraduate Postgraduate
Student Student
We can inherit two classes from the class student- undergraduate students and
postgraduate students (refer figure4). These two classes will have all the
properties of class students and in addition to that it will have even more
specialized members.
When a derived class receives a message to execute a method, it finds the
method in its own class. If it finds the method, then it simply executes it. If the
method is not present, it searches for that method in its superclass. If the method
is found, it is executed; otherwise, an error message is reported.
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
PPS Unit-V DIT,Pimpri
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
PPS Unit-V DIT,Pimpri
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.
16
PPS Unit-V DIT,Pimpri
.
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
PPS Unit-V DIT,Pimpri
Since there exist only one copy of the class variable, any changes made to the class
variable by an object will be reflected in all other objects/instances.
Syntax to access class variable:
className. Class_variable
here className is the name of the class and class_variable is the class variable
defined in the class
Example1:
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
PPS Unit-V DIT,Pimpri
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
self.marks=marks
print(("Serial number is" , self.serial_No)
print ("Marks of a Student is",self.marks)
Example 2:
class student: #class Defination
serial_No=0 # Class Variable
def display(self, marks): # Class Method
student.serial_No+=1
self.marks=marks
print(("Serial number is", self.serial_No)
print ("Marks of a Student is",self.marks)
20
PPS Unit-V DIT,Pimpri
21
PPS Unit-V DIT,Pimpri
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
PPS Unit-V DIT,Pimpri
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)
obj.display()
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
PPS Unit-V DIT,Pimpri
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
PPS Unit-V DIT,Pimpri
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):
self.length=length
self.breadth=breadth
def area(self):
return self.length*self.breadth
26
PPS Unit-V DIT,Pimpri
@classmethod
def Square(cls,side):
return cls(side,side)
S=Rectangle.Square(10)
print("Area=",S.area())
Output:
Area= 100
27