Object Oriented Programming
Object Oriented Programming
1
Objects
2
Object oriented programming (OOP)
3
What are objects?
4
Example: [1,2,3,4] has type list
6
Creating and Using your own types with classes
7
Define your own types
8
What are attributes?
9
Defining how to create an
instance of a class
10
Actually creating an instance of a class
● Data attributes of an
instance are called
instance variables
● Don’t provide argument for
self, Python does this
automatically
11
What is method?
12
Define a method for the Coordinate CLASS
13
How to use a METHOD
● Conventional way
● Equivalent to
14
Print representation of an Object
15
Defining your own print method
16
Wrapping your head around types and classes
… and others
18
The Power of OOP
19
Python Classes and Inheritance
20
Python Classes and Inheritance
● More on classes
○ Getters and setters
○ Information hiding
○ Class variables
● Inheritance
21
Implementing the Class vs Using the Class
● Write code from two different perspectives
Implementing a new object type with Using the new object type in code
a class
● Create instances of the object
● Define the class type
● Define data attributes (WHAT IS ● Do operations with them
the object)
● Define methods (HOW TO use the
object)
22
Class definition of an object type vs
Instance of a class
23
Why use OOP and classes of objects? (recap)
24
Groups of objects have attributes (recap)
● Data attributes
○ How can you represent your object with data
○ What it is
■ For a coordinate: x and y values
■ For an animal: age, name
● Procedural attributes
(behaviour/operations/methods)
○ How can someone interact with the object?
○ What it does?
■ For a coordinate: find distance between two
■ For an animal: make a sound
25
How to define a class (recap)
26
Getter and Setter methods
27
An instance and Dot Notation (recap)
28
Information Hiding
29
Python not great at information hiding
30
Default arguments
31
Hierarchies
32
Hierarchies
33
Inheritance: Parent Class and Subclass
34
Inheritance subclass explained
35
Which methods to use?
36
37
38
Class variables and the Rabbit Subclass
40
Working with your own types
41
Special method to compare two rabbits
● Decide that two rabbits are equal if they have the same
two parents
43