Python Classes and Inheritance
Python Classes and Inheritance
Python Classes and Inheritance
1
Python Classes and Inheritance
● More on classes
○ Getters and setters
○ Information hiding
○ Class variables
● Inheritance
2
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)
3
Class definition of an object type vs
Instance of a class
4
Why use OOP and classes of objects? (recap)
5
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
6
How to define a class (recap)
7
Getter and Setter methods
8
An instance and Dot Notation (recap)
9
Information Hiding
10
Python not great at information hiding
11
Default arguments
12
Hierarchies
13
Hierarchies
14
Inheritance: Parent Class and Subclass
15
Inheritance subclass explained
16
Which methods to use?
17
18
19
Class variables and the Rabbit Subclass
21
Working with your own types
22
Special method to compare two rabbits
● Decide that two rabbits are equal if they have the same
two parents
24