Professional Documents
Culture Documents
6 - Object Oriented Programming
6 - Object Oriented Programming
6 - Object Oriented Programming
1 Introduction to OO
We can view the world as consisting of a set of objects with properties
For example, consider the activity of watching TV:
You want to change channel
You take the remote control and push a button
The TV changes channel
The remote control is a physical object with certain properties such as colour, weight,
size etc.
The remote control can also perform a task – it can do something – it sends messages
to the TV when the buttons are pressed
We do not need to know exactly how the remote control sends messages to the TV in
order to use it – we need only know what we want to do and what buttons to press to
achieve our goal
The buttons are an interface to the remote control and once we understand the
interface we can use the remote control to perform a task for us
The TV is also a physical object with properties
When the TV receives a message from the remote control it changes channel as
required
1 23/02/2024
6.1 Introduction to OO
We can make two complicated devices interact to perform a complex activity
without understanding the internal workings of
either of them.
We form abstractions of objects in order to allow
us to use them – e.g., if you went to a TV shop to
buy a new TV you would not have much difficulty
in using a different type of remote control to test a different brand of TV
2 23/02/2024
6.1 Introduction to OO
We can think of objects as having
A name
Properties (or attributes) associated with it
Messages that it can understand
Examples
A tennis ball, a helicopter, a tree etc.
A number, a word, an image etc.
3 23/02/2024
6.1 Introduction to OO
Python provides a way of forming an abstraction of an object by
encapsulating properties and messages into a single concept
a class
We call the set of properties and messages members of the class
4 23/02/2024
6.1 Introduction to OO
When designing a class think about the objects that will be created
from the class type.
5 23/02/2024
6.2 Python Classes
Python Classes
6 23/02/2024
6.2 Python Classes
Message Passing
Object A Object B
Method A1 Method B1
calls
7 23/02/2024
6.2 Python Classes
Encapsulation
Information hiding
8 23/02/2024
6.2 Python Classes
Defining Python Classes
9 23/02/2024
6.2 Python Classes
Defining Python Classes
Private instance variables of a class begin with two underscore
characters (e.g., __colour ) and cannot be directly accessed
We will create special methods to access private instance variables
Private instance variables are initialized in a special method named
__init__
10 23/02/2024
6.2 Python Classes
Defining Python Classes
11 23/02/2024
6.2 Python Classes
Defining Python Classes
12 23/02/2024
6.2 Python Classes
Defining Python Classes
13 23/02/2024
6.2 Python Classes
Defining Python Classes
We would now like to use this blueprint to create some Car objects
Object Instantiation
First save our class definition as car.py
14 23/02/2024
6.2 Python Classes
Defining Python Classes
Note: the __init__
method is implicitly
called when an object
is created.
carApp.py
Car
Car
17 23/02/2024
6.3 Inheritance
Inheritance
18 23/02/2024
6.3 Inheritance
Inheritance
We can create a new ElectricCar and use the member elements from
the Car superclass:
19 23/02/2024
6.3 Summary
OO Programming
Python Classes
?
Inheritance
20 23/02/2024