0% found this document useful (0 votes)
20 views

Chapter 9

The document discusses object oriented programming concepts in Python like classes, objects, class methods and initialization. It explains that a class is like a blueprint for creating objects with attributes and behaviors. Methods are functions defined inside a class that act on the object's attributes.

Uploaded by

vipan.arora2019
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Chapter 9

The document discusses object oriented programming concepts in Python like classes, objects, class methods and initialization. It explains that a class is like a blueprint for creating objects with attributes and behaviors. Methods are functions defined inside a class that act on the object's attributes.

Uploaded by

vipan.arora2019
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

COMPUTER ENGG 6th SEMESTER

SUBJECT
COMPUTER PROGRAMMING
USING PYTHON

TOPIC: Classes in Python

1 Dr Vipan Arora
Procedural Programming
 Conventional programming using high level
language such as FORTRAN,COBOL,
PASCAL,BASIC and C are known as
Procedure Oriented Languages.
 In a procedural language a problem is viewed
are a sequence of things to be done such as
reading, writing or printing.
 A program in a procedural language is a
sequence of steps.

2 Dr Vipan Arora
 Procedure oriented programming consists of writing
a list of instructions for computer to follow, and
organizing these instructions into groups known as
functions.
 The main attention is given on development of
functions and little attention is given to the data.

3 Dr Vipan Arora
The main characteristics of procedural
oriented programming are
 Emphasis is on doing things (Algorithms) rather than on
data.
 Large program is subdivided into small program called
functions.
 Functions transform the data from one form to another.
 Most of the functions share global data.
 Employs top–down approach in program design.
 Data move openly around the system from one function to
other.

4 Dr Vipan Arora
Object Oriented Programming
 Object Oriented Programming is commonly
used approach for designing large and complex
real world applications.
 Object oriented approach unlike procedural
approach lays more emphasis on data rather
than functions and does not allow data to be
accessed freely in the system.
 It ties the data and functions that operate on
it and protects it from other functions.

5 Dr Vipan Arora
 Object Oriented Programming allows decomposition of a
problem into a number of entities called Objects and then
builds data and functions around these objects.

6 Dr Vipan Arora
Some of the features of object-oriented
programming are as follows
 Emphasis is on data rather than procedure.
 Programs are divided into what are known as objects.
 Data structures are designed such that they characterize the
objects.
 Functions that operate on the data of an object are tied together
in the data structure.
 Data is hidden and cannot be accessed by external
functions.
 Objects may communicate with each other through functions.
 New data and functions can be easily added whenever
necessary.
 Follows bottom-up approach in program design.
7 Dr Vipan Arora
Basic Concepts of Object Oriented
Programming

8 Dr Vipan Arora
Object
 An object is an entity that has state, behaviour and
identity. Objects are used to model real world entities
that we find in everyday life.
 Some common examples of object are book, clock, apple,
car etc.Every object will have data structures called
attributes and behaviour called operations.
 Consider the object Account having attributes Account
Number, Name, Balance and Operations : Deposit,
Withdraw and Enquire.

9 Dr Vipan Arora
Object?
 An object is an entity that has state, behavior and
identity. Objects are used to model real world entities
that we find in everyday life. Some common examples
of object are book, clock, apple, car etc.
 Every object will have data structures called attributes
and behavior called operations.

10 Dr Vipan Arora
Consider the object Account
 Attributes : Account Number, Name, Balance
 Operations : Deposit, Withdraw and Enquire.

11 Dr Vipan Arora
Different styles of representation
Account object

To sum up, each object possess the following characteristics


• Each object has an identity and is distinguishable.
• Data and functions are binded together to form an object.
• Objects in the system communicate with each other using the
concept of message passing.

12 Dr Vipan Arora
Class
 A class is group of objects with same attributes and
common behaviours. It is just a template or a
blueprint to create objects.

13 Dr Vipan Arora
Similarly other examples of class

Class Computer
Attributes Price, Brand, RAM size, Monitor Resolution

Behaviour Display ( ), Printing ( ), Processing ( )

Class Employee
Attributes Ecode, Ename, Salary, Loan-taken

Behaviour Salary_cal ( ), Display ( ), Loan ( )

14 Dr Vipan Arora
Difference between object and class
Sno Object Class

1 Data and functions that operate on that data are A collection of similar type of objects is called a class.
binded together to form an object.

2 Object are real world entities. Class is just a specification or a logical entity.

3 Objects occupy some space in memory. Class doesn’t consume space in memory.

4 Object is an instance of a class. Class is a set of objects that share common attitudes and
behaviours.

5 Table, chair, sofa are examples of objects. Furniture is an example of class which has table, chair, sofa
objects.

15 Dr Vipan Arora
CREATING CLASS IN PYHTON
 Class creates a user-defined data structure, which holds its
own data members and member functions, which can be
accessed and used by creating an instance of that class. A
class is like a blueprint for an object.
Some points on Python class:
 Classes are created by keyword class.
 Attributes are the variables that belong to a class.
 Attributes are always private and can be accessed using
the dot (.) operator. Eg.: Myclass.Myattribute

16 Dr Vipan Arora
17 Dr Vipan Arora
Class Definition Syntax
class ClassName:
# Statement-1
.
.
.
# Statement-N
Create a class named MyClass, with a property named x:
class first:
def f1():
print('HELLO WORLD')

18 Dr Vipan Arora
Class Objects
 An Object is an instance of a Class. A class is like a
blueprint while an instance is a copy of the class
with actual values.
 An object consists of :
 State: It is represented by the attributes of an object. It
also reflects the properties of an object.
 Behavior: It is represented by the methods of an object. It
also reflects the response of an object to other objects.
 Identity: It gives a unique name to an object and enables
one object to interact with other objects.

19 Dr Vipan Arora
20 Dr Vipan Arora
Declaring Objects (Also called
instantiating a class)
 When an object of a class is created, the class is said to be
instantiated. All the instances share the attributes and the
behavior of the class. But the values of those attributes,
i.e. the state are unique for each object. A single class may
have any number of instances.

21 Dr Vipan Arora
class first:
def f1(self):
print('HELLO WORLD')
x=first()
x.f1()

22 Dr Vipan Arora
Create a class named Employee having two
properties name and age
class Employee:
name = “Namya"
age = 26
Create Class Object
The e object is an instance of the Employee class. It has the data
attributes described by the Employee class.
class Employee:
name = "Namya"
age = 26

e = Employee()
print(e.name, e.age)
print(type(e))
23 Dr Vipan Arora
Initializing the Object
 The __init__() method is commonly known as
an initializer method because it initializes the object's
data attributes.
 An object of a class usually initialized by implementing
the __init__() method.
 When an object is created, Python first creates an empty
object and then calls the __init__() method for that new
object.

24 Dr Vipan Arora
Example
 The Employee class's __init__ method is called, and the
self parameter will reference the e object.
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age

e = Employee(“Namya", 36)
print(e.name, e.age)

25 Dr Vipan Arora
Empty Class
 The pass keyword allows to create empty class for data
storage.
class PermanentEmployee:
pass

emp = PermanentEmployee()
emp.name = “Namya"
emp.age = 10

26 Dr Vipan Arora
Object Method
 A method is formatted identically to a function. It starts
with the def keyword, followed by a space, and the name
of the method.
 Methods work in exactly the same way as simple
functions, with one crucial exception: a method's first
argument always receives the instance object.
 Those methods that do not take any arguments have at
least the argument as self.

27 Dr Vipan Arora
Example
 When the method executes, the self parameter will reference
the emp object.
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age

def details(self):
print("Employee Name:", self.name)
print("Employee Age:", self.age)

emp = Employee(“Namya", 36)


emp.details()
28 Dr Vipan Arora
class Dog:
# A simple class attribute
attr1 = "mammal"
attr2 = "dog"

# A sample method
def fun(self):
print("I'm a", self.attr1)
print("I'm a", self.attr2)

# Driver code Object instantiation


Rodger = Dog()

# Accessing class attributes and method through objects


print(Rodger.attr1)
Rodger.fun()
29 Dr Vipan Arora
Data Encapsulation
 Data Encapsulation is a mechanism that binds together
data and functions that manipulate that data.
 It keeps the data safe from any external interference
and misuse. In other words, encapsulation is wrapping
up of data and related functions in a single unit called
object.

30 Dr Vipan Arora
Encapsulation
• Encapsulation is the mechanism that binds together the code
and the data it manipulates, and keeps both safe from outside
interference and misuse.

Public variables and methods A Class

Private variables
and methods

Public variables is not recommended

31 Dr Vipan Arora
Data Hiding
 Data hiding can be defined as a mechanism of hiding
the data of a class from the outside world (other
classes) so that any access to it either intentionally or
unintentionally can’t modify the data.

32 Dr Vipan Arora
Data Abstraction
 Abstraction is a process that involves identifying the
essential features without including the internal
details.

33 Dr Vipan Arora
Inheritance
 Inheritance is a process of deriving a new class from the
existing class without modifying it.
 The new class is called the derived class or subclass or
child class and the existing class is called the base class
or superclass or parent class.

34 Dr Vipan Arora
35 Dr Vipan Arora
Inheritance: Example
Account

Checking Mortgage Loan

36 Dr Vipan Arora
Polymorphism
 Polymorphism can be defined as the ability to use the
same name for two or more related but technically
different tasks.
 In other words, Polymorphism can also be defined as one
interface that can be used to perform related but different
tasks.

37 Dr Vipan Arora
38 Dr Vipan Arora
Dynamic Binding
 Binding refers to the linking of a function call to the code
of the function to be executed in response to the function
call. Binding is of two types
(a) Static Binding or Early Binding
(b) Dynamic Binding or Late Binding

39 Dr Vipan Arora
 In Static Binding, the linking of function call to the code
of the function to be executed in response to the
function call is made at the compile time.
 In Dynamic binding, the linking of function call to the
code of the function to be executed in response to the
function call is made at run time.

40 Dr Vipan Arora
Message Passing
 Message passing involves sending a message from one
object to another object in the system.
 A message to an object is treated as a request for the
execution of its function.
 On receiving the message, the suitable function of the
object is invoked and the desired results are generated.

41 Dr Vipan Arora
Thanks
42 Dr Vipan Arora

You might also like