KEMBAR78
Week 9 Lecture Slides | PDF | Object Oriented Programming | Method (Computer Programming)
0% found this document useful (0 votes)
28 views43 pages

Week 9 Lecture Slides

This document provides an overview of object oriented programming concepts including classes, objects, encapsulation, and inheritance. It discusses the differences between procedural and object oriented programming, defining classes and objects, and creating class instances in Python using the __init__() method. Key concepts covered are that classes provide a logical grouping of data and methods, objects are instances of classes, and the __init__() method initializes an object's attributes when it is created.

Uploaded by

Achyut Parajuli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views43 pages

Week 9 Lecture Slides

This document provides an overview of object oriented programming concepts including classes, objects, encapsulation, and inheritance. It discusses the differences between procedural and object oriented programming, defining classes and objects, and creating class instances in Python using the __init__() method. Key concepts covered are that classes provide a logical grouping of data and methods, objects are instances of classes, and the __init__() method initializes an object's attributes when it is created.

Uploaded by

Achyut Parajuli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Lecture 9:

Object Oriented Programming

Hrishav Tandukar
hrishav.tandukar@islingtoncollege.edu.np

CS4051 Fundamentals of Computing


Contents
• Object oriented programming (OOP)
• OOP concepts
• Classes & objects
• OOP in python

CS4051 Fundamentals of Computing 2


Procedural vs Object Oriented Programming
• Procedural programming creates a step by step program that guides the application
through a sequence of instructions. Each instruction is executed in order.
• Procedural programming also focuses on the idea that all algorithms are executed with
functions and data that the programmer has access to is able to change

CS4051 Fundamentals of Computing 3


Procedural vs Object Oriented Programming
• Procedural programming creates a step by step program that guides the application
through a sequence of instructions. Each instruction is executed in order.
• Procedural programming also focuses on the idea that all algorithms are executed with
functions and data that the programmer has access to is able to change.
• Object-Oriented programming is much more similar to the way the real world works.
Each program is made up of many entities called objects.
• Instead, to access data it must be requested, just like people must ask one another for
information; we cannot see inside each other’s heads.

CS4051 Fundamentals of Computing 4


Object Oriented Programming
• Based on the concept of classes whose instances are called objects
• A class is simply a logical grouping of data and functions
• Classes provide a way to structure a program, its like a blueprint

CS4051 Fundamentals of Computing 5


Object Oriented Programming
• Based on the concept of classes whose instances are called objects
• A class is simply a logical grouping of data and functions
• Classes provide a way to structure a program, its like a blueprint
• Classes store data in form of attributes
• Each class has its own methods which perform some operations

CS4051 Fundamentals of Computing 6


Object Oriented Programming
• Based on the concept of classes whose instances are called objects
• A class is simply a logical grouping of data and functions
• Classes provide a way to structure a program, its like a blueprint
• Classes store data in form of attributes
• Each class has its own methods which perform some operations
• Useful in modeling real world entities
• Code written in OOP is reusable resulting in less code

CS4051 Fundamentals of Computing 7


OOP concepts
• There are four major principles/concepts in Object Oriented Programming
• Encapsulation – bundling together data and methods
• Data abstraction – hiding the data/details
• Inheritance – inheriting from parents
• Polymorphism – having multiple forms/meaning

CS4051 Fundamentals of Computing 8


OOP concepts
• There are four major principles/concepts in Object Oriented Programming
• Encapsulation – bundling together data and methods
• Data abstraction – hiding the data/details
• Inheritance – inheriting from parents
• Polymorphism – having multiple forms/meaning
• We’ll see what the first two terms mean in this lecture, the next two will be discussed in
the next lecture

CS4051 Fundamentals of Computing 9


Classes and Objects
• A class is a definition of objects which share common structure, properties and
behavior
• Instances of classes are objects that are created which follow the definition given
inside the class
• Objects are the basic run-time entities in an object-oriented system
• Objects have
• data (i.e., attributes)
• behaviors (i.e., methods)

CS4051 Fundamentals of Computing 10


Classes and Objects

CS4051 Fundamentals of Computing 11


Classes and Objects
• Think of a taxi as a class
• A taxi has data related to it which can be
named as attributes
• name of driver
• car model
• number of passengers
• current location

CS4051 Fundamentals of Computing 12


Classes and Objects
• Think of a taxi as a class
• A taxi has data related to it which can be
named as attributes
• name of driver
• car model
• number of passengers
• current location

CS4051 Fundamentals of Computing 13


Classes and Objects
• Think of a taxi as a class • The things that a taxi can do are
• A taxi has data related to it which can be ▪ move
named as attributes ▪ pick up passengers
• name of driver ▪ drop off passengers
• car model ▪ go on duty
• number of passengers ▪ go off duty
• current location

CS4051 Fundamentals of Computing 14


Classes and Objects
• Think of a taxi as a class • The things that a taxi can do are
• A taxi has data related to it which can be ▪ move
named as attributes ▪ pick up passengers
• name of driver ▪ drop off passengers
• car model ▪ go on duty
• number of passengers ▪ go off duty
• current location

CS4051 Fundamentals of Computing 15


Everything is an object in python!
• Python supports many different kinds of data
1234 3.1415 “Hello” [3, 4, 2, 14, 7]
{“KTM”: “Kathmandu”, “NY”: “New York”}
• each is an object, and every object has:
• a type
• an internal data representation
• a set of methods/functions for interaction with the objects
• 1234 is an instance of int while “Hello” is an instance of string

CS4051 Fundamentals of Computing 16


Creating and using classes
• creating a class involves
• defining the class name
• defining its attributes
• defining certain behaviors
• for example, code has already been written to implement a list class

CS4051 Fundamentals of Computing 17


Creating and using classes
• creating a class involves
• defining the class name
• defining its attributes
• defining certain behaviors
• for example, code has already been written to implement a list class
• using the class involves
• creating new instances of classes
• doing operations on the instances
• for example, L =[1, 2] and L.append(3)

CS4051 Fundamentals of Computing 18


Defining a class
• While def is used to define a function, class is used to define a class
class Customer():
# define attributes and methods
• similar to def, indent code to indicate which statements are part of the class
definition

CS4051 Fundamentals of Computing 19


Defining how to create an instance of a class
• first have to define the initializer (constructor) method which says how to create an
instance of the class
• use a special method called __init__ to initialize the instance, __init__ serves
as the constructor for the class
class Customer():

CS4051 Fundamentals of Computing 20


Defining how to create an instance of a class
• first have to define the initializer (constructor) method which says how to create an
instance of the class
• use a special method called __init__ to initialize the instance, __init__ serves
as the constructor for the class
class Customer():
def __init__(self, name, balance):
self.name = name
self.balance = balance

CS4051 Fundamentals of Computing 21


Defining how to create an instance of a class
• first have to define the initializer (constructor) method which says how to create an
instance of the class
• use a special method called __init__ to initialize the instance, __init__ serves
as the constructor for the class parameter to refer to an
class Customer(): instance of the customer class

def __init__(self, name, balance):


self.name = name
self.balance = balance

CS4051 Fundamentals of Computing 22


Defining how to create an instance of a class
• first have to define the initializer (constructor) method which says how to create an
instance of the class
• use a special method called __init__ to initialize the instance, __init__ serves
as the constructor for the class parameter to refer to an
class Customer(): instance of the customer class

def __init__(self, name, balance):


self.name = name data that initializes a
self.balance = balance customer object

CS4051 Fundamentals of Computing 23


Defining how to create an instance of a class
• first have to define the initializer (constructor) method which says how to create an
instance of the class
• use a special method called __init__ to initialize the instance, __init__ serves
as the constructor for the class parameter to refer to an
class Customer(): instance of the customer class

def __init__(self, name, balance):


self.name = name data that initializes a
two
attributes for self.balance = balance customer object
every
customer
object

CS4051 Fundamentals of Computing 24


__init__ and self
• __init__ is the initializer (constructor) method
• the __init__ method can take any number of arguments, however the first
argument self in the definition of __init__ is special

CS4051 Fundamentals of Computing 25


__init__ and self
• __init__ is the initializer (constructor) method
• the __init__ method can take any number of arguments, however the first
argument self in the definition of __init__ is special
• self is a reference to the current instance of the class
• in __init__, self refers to the object currently being created

CS4051 Fundamentals of Computing 26


self argument

Customer
Class

CS4051 Fundamentals of Computing 27


self argument
Customer
Object
Name, Balance

Customer
Class

CS4051 Fundamentals of Computing 28


self argument
Customer
Object
Name, Balance

Customer
Class Customer
Object
Name, Balance

CS4051 Fundamentals of Computing 29


self argument
Customer
Object
Customer Name, Balance Customer
Object Object
Name, Balance Name, Balance

Customer
Customer
Object
Class Customer
Object
Name, Balance
Name, Balance

Customer Customer
Object Object
Name, Balance Name, Balance

CS4051 Fundamentals of Computing 30


Defining methods
• methods are functions that work only with a certain class
• while defining functions too, self is required as the first argument
• self is a reference to a specific instance of the class whose method is being called
• the “.” operator is used to access any attribute or method of an object

CS4051 Fundamentals of Computing 31


Defining methods
class Customer():
def __init__(self, name, balance):
self.name = name
self.balance = balance
def withdraw(self, amount):
self.balance -= amount
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance

CS4051 Fundamentals of Computing 32


Defining methods
class Customer():
def __init__(self, name, balance):
self.name = name
self.balance = balance
def withdraw(self, amount): a function that takes a
self.balance -= amount certain amount and
deducts that amount from
return self.balance the initial balance
def deposit(self, amount):
self.balance += amount
return self.balance

CS4051 Fundamentals of Computing 33


Defining methods
class Customer():
def __init__(self, name, balance):
self.name = name
self.balance = balance
def withdraw(self, amount): a function that takes a
self.balance -= amount certain amount and
deducts that amount from
return self.balance the initial balance
def deposit(self, amount): a function that takes a
self.balance += amount certain amount and adds
that amount to the initial
return self.balance balance

CS4051 Fundamentals of Computing 34


Creating instances of a class & calling methods
C1 = Customer(“Mike”, 2000) # create a customer
print(C1.withdraw(1000)) # prints 1000
print(C1.deposit(1000)) # prints 2000

CS4051 Fundamentals of Computing 35


Creating instances of a class & calling methods
C1 = Customer(“Mike”, 2000) # create a customer
print(C1.withdraw(1000)) # prints 1000
print(C1.deposit(1000)) # prints 2000
C2 = Customer(“John”, 9000) # create a customer
print(C2.withdraw(1000)) # prints 8000
print(C2.deposit(2000)) # prints 10000

CS4051 Fundamentals of Computing 36


Creating instances of a class & calling methods
C1 = Customer(“Mike”, 2000) # create a customer
print(C1.withdraw(1000)) # prints 1000
print(C1.deposit(1000)) # prints 2000
C2 = Customer(“John”, 9000) # create a customer
print(C2.withdraw(1000)) # prints 8000
print(C2.deposit(2000)) # prints 10000

while creating objects and calling methods, don’t provide self, python
does that automatically

CS4051 Fundamentals of Computing 37


Accessing & modifying attributes
C1 = Customer(“Mike”, 2000) # create a customer
print(C1.name) # prints Mike
print(C1.balance) # prints 2000

C1.balance = 3000 # set new balance


print(C1.balance) # prints new balance 3000

CS4051 Fundamentals of Computing 38


Encapsulation and Data Abstraction
• Encapsulation is the process of binding together of data and methods that operate on
the data
• Encapsulation is when an instance of a class is created, when related attributes and
methods are bundled up

CS4051 Fundamentals of Computing 39


Encapsulation and Data Abstraction
• Encapsulation is the process of binding together of data and methods that operate on
the data
• Encapsulation is when an instance of a class is created, when related attributes and
methods are bundled up
• Data abstraction is achieved through encapsulation
• Encapsulation does this by restricting access to some of the object’s components,
meaning the internal representation of an object cannot be seen from outside the
object’s definition

CS4051 Fundamentals of Computing 40


The power of OOP
• bundle together objects that share
• common attributes and
• methods that operate on those attributes
• use abstraction to make a distinction between how to implement an object vs how to
use the object
• create our own classes of objects on top of python’s basic classes

CS4051 Fundamentals of Computing 41


End of Lecture 9

CS4051 Fundamentals of Computing 42


Thank you !
Any questions ?

CS4051 Fundamentals of Computing 43

You might also like