Objects and Classes
1 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Objectives
❑ To describe objects and classes (§9.2)
❑ To define classes with data fields and methods (§9.2.1).
❑ To construct an object using a constructor that invokes the initializer to create
and initialize data fields (§9.2.2).
❑ To access the members of objects using the dot operator (.) (§9.2.3).
❑ To reference an object itself with the self parameter (§9.2.4).
❑ To use UML graphical notation to describe classes and objects (§9.3).
❑ To distinguish between immutable and mutable objects (§9.5).
❑ To hide data fields to prevent data corruption (§9.6).
❑ To apply class abstraction and encapsulation to software development (§9.7).
❑ To explore the differences between the procedural paradigm and the object-
oriented paradigm (§9.8).
❑ To define special methods for operators (§9.9).
2 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Introduction
❑ Object-oriented programming enables you to develop large-scale software and
graphical user interface (GUIs) effectively.
❑ Having learned the material in the preceding chapters, you are now able to
solve many programming problems by using selections, loops, functions, and
lists. However, these features are not sufficient for developing a GUI, or a
large-scale software system.
❑ You can create GUI objects like this using object-oriented programming.
3 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Defining Classes for Objects
❑ A class is a blueprint or a template for producing objects. It defines the
properties and behaviors for objects.
❑ An object has a unique identity, state, and behavior.
❑ An object’s identity is like a person’s national ID number. Python
automatically assigns each object a unique id for identifying the object at
runtime.
❑ An object’s state (also known as its properties or attributes) is represented by
variables, called data fields.
❑ A circle object, for example, has a data field radius, which is a property that
characterizes a circle.
❑ A student object has the data fields name and dob, which are properties that
characterize a student.
4 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Defining Classes for Objects
❑ Python uses methods to define an object’s behavior (also known as its actions).
❑ Objects of the same kind are defined by using a common class. The
relationship between classes and objects is analogous to that between an apple-
pie recipe and apple pies. You can make as many apple pies (objects) as you
want from a single recipe (class).
❑ An object is an instance of a class, and you can create many instances of a
class. Creating an instance of a class is referred to as instantiation. The terms
object and instance are often used interchangeably.
5 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Defining Classes for Objects
❑ The following diagram shows a class named Circle and its three objects.
6 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Defining Classes
❑ A class provides a special method, __init__( ). This method, known as an
initializer, is invoked to initialize a new object’s state when it is created.
❑ An initializer can perform any action, but initializers are designed to perform
initializing actions, such as creating an object’s data fields with initial values.
❑ Python uses the following syntax to define a class:
7 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Defining Classes
❑ The following code defines the Circle class:
8 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Constructing Objects
❑ Once a class is defined, you can create objects from the class with a
constructor. The constructor does two things:
❑ It creates an object in the memory for the class.
❑ It invokes the class’s __init__ method to initialize the object.
❑ All methods, including the initializer, have the first parameter self. This
parameter refers to the object that invokes the method.
❑ The self parameter in the __init__ method is automatically set to reference the
object that was just created.
❑ You can specify any name for this parameter, but by convention self is usually
used.
❑ The syntax for a constructing an object from a give class is:
ClassName(arguments)
9 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Constructing Objects
10 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Accessing Members of Objects
❑ In OOP terminology, an object’s member refers to its data fields and methods.
Data fields are also called instance variables, because each object (instance)
has a specific value for a data field.
❑ Methods are also called instance methods, because a method is invoked by an
object (instance) to perform actions on the object such as changing the values
in data fields for the object.
11 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
The self Parameter
❑ self is a parameter that references the object itself. When a method is called on
an object, self is set to the object.
❑ Using self, you can access object’s members in a class definition. For example,
you can use the syntax self.x to access the instance variable x and syntax
self.m1() to invoke the instance method m1 for the object self in a class
❑ The scope of an instance variable is the entire class once it is created.
12 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Example: Using Classes
13 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Example: Using Classes
14 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Objects vs. Variables and Copying Objects
❑ A variable that appears to hold an object contains a reference to that object.
❑ To copy the contents of c2 to c1, you need to copy the data fields from c2 to c1
using the following statement:
15 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Objects vs. Variables and Copying Objects
16 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
UML Class Diagrams
❑ UML class diagrams use graphical notation to describe classes.
❑ They are language independent; that is, other programming languages use this
same modeling and notation.
17 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
UML Class Diagrams
❑ The UML diagram serves as the contract (template) for the client so that it will
know how to use the class.
❑ The method definition in the class always has the special self parameter, but
don’t include it in the UML diagram.
❑ The __init__ method does not need to be listed in the UML diagram either,
because it is invoked by the constructor and its parameters are the same as the
constructor’s parameters.
18 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
UML Class Diagrams
19 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
The TV Class
20 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
The TV Class
21 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Using Classes from the Python Library: the
datetime Class
22 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Using Classes from the Python Library: the
datetime Class
23 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Immutable Objects vs. Mutable Objects
❑ When passing a mutable object to a function, the function may change the contents
of the object. Recall that numbers and strings are immutable objects in Python.
❑ When you pass an object to a function, the reference of the object is passed to the
function. myCircle and c both point to the same object.
24 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Hiding Data Fields
❑ Making data fields private protects data and makes the class easy to maintain.
❑ You can access data fields via instance variables directly from an object.
❑ However, direct access of a data field in an object is not a good practice:
❑ For example, channel in the TV class has a value between 1 and 120, but it may be
mistakenly set to an arbitrary value (e.g., tv1.channel = 125).
❑ To prevent direct modifications of data fields, don’t let the client directly access
data fields. This is known as data hiding. This can be done by defining private
data fields.
❑ In Python, the private data fields are defined with two leading underscores.
You can also define a private method named with two leading underscores.
25 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Hiding Data Fields
❑ Private data fields and methods can be accessed within a class, but they cannot
be accessed outside the class. To make a data field accessible for the client,
provide a getter (accessor) method to return its value. To enable a data field to
be modified, provide a setter (mutator) method to set a new value.
26 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Hiding Data Fields
27 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Hiding Data Fields
❑ Tip: If a class is designed for other programs to use, to prevent data from being
tampered with and to make the class easy to maintain, define data fields as
private. If a class is only used internally by your own program, there is no need
to hide the data fields.
❑ Name private data fields and methods with two leading underscores, but don’t
end the name with more than one underscores. The names with two leading
underscores and two ending underscores have special meaning in Python. For
example, __radius is a private data field, but, __radius__ is not a private data
field.
28 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Class Abstraction and Encapsulation
❑ Class abstraction is a concept that separates class implementation from the use
of a class. The class implementation details are invisible from the user. This is
known as class encapsulation.
❑ Encapsulation combines data and methods into a single object and hides the
data fields and method implementation from the user.
❑ Your personal computer has many components—a CPU, memory, disk,
motherboard, fan, and so on. To get the components to work together, you need
to know only how each component is used and how it interacts with others. You
don’t need to know how the components work internally. The internal
implementation is encapsulated and hidden from you.
29 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Class Abstraction and Encapsulation
30 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Object-Oriented Thinking
Computing the BMI:
❑ The code as it is cannot be reused in
other programs. To make it
reusable, define a standalone
function to compute body mass
index, as follows:
def getBMI(weight, height):
❑ This function is useful for
computing body mass index for a
specified weight and height.
However, it has limitations.
Suppose you need to associate the
weight and height with a person’s
name and birth date
31 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Object-Oriented Thinking
❑ Computing the BMI (the OOP way)
32 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Object-Oriented Thinking
33 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
34 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Operator Overloading and Special Methods
❑ Python allows you to define special methods for operators and functions to
perform common operations.
35 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Operator Overloading and Special Methods
36 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
EXAMPLE: The Rational Class
❑ A rational number has a numerator and a denominator in the form a/b, where
a is the numerator and b is the denominator. For example, 1/3, 3/4, and 10/4 are
rational numbers.
❑ A rational number cannot have a denominator of 0, but a numerator of 0 is
fine. Every integer i is equivalent to a rational number i/1
37 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
EXAMPLE: The Rational Class
38 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
EXAMPLE: The Rational Class
❑ The comparison operators <, <=, ==, !=, >, and >= can also be implemented
using the __cmp__ (self, other) method. This method returns a negative integer
if self < other, zero if self == other, and a positive integer if self > other
39 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
EXAMPLE: The Rational Class
40 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Inheritance
❑ Object-oriented programming (OOP) allows you to define new classes from
existing classes. This is called inheritance.
41 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
42 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Inheritance
43 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Inheritance
44 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Inheritance
45 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Inheritance
46 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Overriding Methods
❑ To override a method, the method must be defined in the subclass using the
same header as in its superclass.
❑ The following overrides the __str__ method in the Circle class:
47 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
Polymorphism and Dynamic Binding
❑ The word "polymorphism" means "many forms", and in programming it refers
to methods with the same name that can be executed on many objects or
classes.
48 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024
The isinstance Function
49 EE202: Object Oriented Programming (by Dr. Abdullah Balamash) Spring 2024