ITS 16163 INTRODUCTION TO
COMPUTER PROGRAMMING
Object-Oriented Programming
(OOP)
3
 Python has been an object-oriented language
since it existed.
 Because of this, you can create classes and
object.
 Object-Oriented Programming it’s a modern
methodology that’s been embraced by the
software industry.
 It’s used in the creation of the majority of new,
commercial software.
 The basic building block in OOP is the software
object often just called an object.
Object-Oriented Programming (OOP)
4
 What you often want to represent in your
programs anything from a checking account to an
alien spacecraft are real-life objects.
 OOP lets you represent these real-life objects as
software
 Like real-life objects, software objects combine:
 characteristics called attributes
 behaviors called methods
For example: if you were to create an alien
spacecraft object, its attributes could include its
location and energy level, while its methods could
include its ability to move or fire its weapons.
UNDERSTANDING OBJECT-ORIENTED
BASICS
5
 Objects are created (or instantiated) from a
definition called a class
 Class is a programming code that can define
attributes and methods. Like blueprints.
 A class isn’t an object, it’s a design for one.
For Example: An engineer can create many
houses from the same blueprint, a programmer can
create many objects from the same class.
 As a result, each object (also called an instance)
instantiated from the same class will have a
similar structure. So, if you have a checking
account class, you could use it to create multiple
checking account objects. But each object has
UNDERSTANDING OBJECT-ORIENTED
BASICS
6
 The class statement creates a new class
definition. The name of the class immediately
follows the keyword class followed by a colon.
Example:
class Employee:
 You’ll notice that my class name begins with a
capital letter. Python doesn’t require this, but it’s
the standard convention, so you should begin all
your class names with a capital letter.
 The next line is a docstring, which documents the
class. A good docstring describes the kind of
objects a class can be used to create.
'Common base class for all employees‘
OOP Creating Classes
7
 The first method __init__() is a special method,
which is called class constructor or initialization
method that Python calls when you create a new
instance of this class.
def __init__(self, name, salary):
 You can think of methods as functions associated with
an object.
def displayEmployee(self):
 The displayEmployee() method prints the string:
"Name : ", and "Salary: “
 You’ll notice that displayEmployee () has one
parameter, self (which it doesn’t happen to use).
Every instance method a method that every object of
a class has must have a special first parameter, called
OOP Creating Method
8
 To create instances of a class, you call the class using
class name and pass in whatever arguments
its __init__ method accepts.
Example: Class Employee:
"This would create first object of Employee class"
emp1 = Employee("Zara", 2000)
 This line creates a brand-new object of the Employee
class and assigns it to the variable emp1.
 Once you’ve written a class, creating multiple objects
is very easy.
Example:
Emp2 = Employee("Manni", 5000) Creating
multiple objects
OOP Creating Instance Objects
9
Example Code: critter_caretaker
Example Code: OOP
 Use the example code:
 Execute the examples under Chapter 7 OOP
Example (trinket.io)
Controlling Attribute Access

ITS-16163: Module 7 Introduction to Object-Oriented Programming Basics

  • 1.
    ITS 16163 INTRODUCTIONTO COMPUTER PROGRAMMING
  • 2.
  • 3.
    3  Python hasbeen an object-oriented language since it existed.  Because of this, you can create classes and object.  Object-Oriented Programming it’s a modern methodology that’s been embraced by the software industry.  It’s used in the creation of the majority of new, commercial software.  The basic building block in OOP is the software object often just called an object. Object-Oriented Programming (OOP)
  • 4.
    4  What youoften want to represent in your programs anything from a checking account to an alien spacecraft are real-life objects.  OOP lets you represent these real-life objects as software  Like real-life objects, software objects combine:  characteristics called attributes  behaviors called methods For example: if you were to create an alien spacecraft object, its attributes could include its location and energy level, while its methods could include its ability to move or fire its weapons. UNDERSTANDING OBJECT-ORIENTED BASICS
  • 5.
    5  Objects arecreated (or instantiated) from a definition called a class  Class is a programming code that can define attributes and methods. Like blueprints.  A class isn’t an object, it’s a design for one. For Example: An engineer can create many houses from the same blueprint, a programmer can create many objects from the same class.  As a result, each object (also called an instance) instantiated from the same class will have a similar structure. So, if you have a checking account class, you could use it to create multiple checking account objects. But each object has UNDERSTANDING OBJECT-ORIENTED BASICS
  • 6.
    6  The classstatement creates a new class definition. The name of the class immediately follows the keyword class followed by a colon. Example: class Employee:  You’ll notice that my class name begins with a capital letter. Python doesn’t require this, but it’s the standard convention, so you should begin all your class names with a capital letter.  The next line is a docstring, which documents the class. A good docstring describes the kind of objects a class can be used to create. 'Common base class for all employees‘ OOP Creating Classes
  • 7.
    7  The firstmethod __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class. def __init__(self, name, salary):  You can think of methods as functions associated with an object. def displayEmployee(self):  The displayEmployee() method prints the string: "Name : ", and "Salary: “  You’ll notice that displayEmployee () has one parameter, self (which it doesn’t happen to use). Every instance method a method that every object of a class has must have a special first parameter, called OOP Creating Method
  • 8.
    8  To createinstances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts. Example: Class Employee: "This would create first object of Employee class" emp1 = Employee("Zara", 2000)  This line creates a brand-new object of the Employee class and assigns it to the variable emp1.  Once you’ve written a class, creating multiple objects is very easy. Example: Emp2 = Employee("Manni", 5000) Creating multiple objects OOP Creating Instance Objects
  • 9.
    9 Example Code: critter_caretaker ExampleCode: OOP  Use the example code:  Execute the examples under Chapter 7 OOP Example (trinket.io) Controlling Attribute Access