Dis 06:
Object Oriented Programming
TA: Anita Cu
anitacu5@berkeley.edu
Agenda
0 ) Administrivia
1 ) Dictionaries
2 ) OOP
3 ) Inheritance
2
Administrivia
3
Dictionaries
1
Dictionaries
5
Dictionaries
6
Dictionaries
7
Dictionaries
8
Dictionaries
9
Dictionaries
10
Dictionaries
11
Dictionaries
12
Dictionaries
13
Dictionary Methods
14
Dictionary Methods
15
Dictionary Methods
16
Dictionary Methods
17
Dictionary Methods
18
Dictionary Methods
19
Iterating Through Contents of
a Dictionary
20
Iterating Through Contents of
a Dictionary
21
Iterating Through Contents of
a Dictionary
22
Iterating Through Contents of
a Dictionary
23
Object Oriented Programming
(OOP)
2
OOP Goal
25
OOP Terminology
26
OOP Terminology
27
OOP Terminology
28
OOP Terminology
29
OOP Terminology
30
OOP Terminology
31
OOP Terminology
32
OOP Terminology
33
OOP Terminology
34
OOP Terminology
35
Method vs. Function
36
Method vs. Function
37
Important Notes!
38
Mini Quiz!
class Student:
def __init__(self, name):
self.name = name
def attend_lecture(self, subject):
print(self.name + “ is learning about “ + subject)
anita = Student(“Anita”)
39
Mini Quiz!
Q: Which of these are valid method calls?
> anita.attend_lecture(“OOP”)
> anita.attend_lecture(anita, “OOP”)
> Student.attend_lecture(“OOP”)
> Student.attend_lecture(anita, “OOP”)
40
Mini Quiz!
Q: Which of these are valid method calls?
> anita.attend_lecture(“OOP”)
> anita.attend_lecture(anita, “OOP”)
> Student.attend_lecture(“OOP”)
> Student.attend_lecture(anita, “OOP”)
41
Mini Quiz!
Q: Which of these are valid method calls?
> anita.attend_lecture(“OOP”)
> anita.attend_lecture(anita, “OOP”)
attend_lecture takes 2 arguments, but this call passes in 3 arguments! What are the 3 arguments?
> Student.attend_lecture(“OOP”)
> Student.attend_lecture(anita, “OOP”)
42
Mini Quiz!
Q: Which of these are valid method calls?
> anita.attend_lecture(“OOP”)
> anita.attend_lecture(anita, “OOP”)
attend_lecture takes 2 arguments, but this call passes in 3 arguments! What are the 3 arguments?
> Student.attend_lecture(“OOP”)
No specific instance for this method was given.
> Student.attend_lecture(anita, “OOP”)
43
Mini Quiz!
Q: Which of these are valid method calls?
> anita.attend_lecture(“OOP”)
> anita.attend_lecture(anita, “OOP”)
attend_lecture takes 2 arguments, but this call passes in 3 arguments! What are the 3 arguments?
> Student.attend_lecture(“OOP”)
No specific instance for this method was given.
> Student.attend_lecture(anita, “OOP”)
44
Inheritance
2
Let’s reuse even more code!
Current Implementation
class Dog(object):
def __init__(self, name, owner):
self.name = name
self.owner = owner
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name + “ says Woof!”)
46
Current Implementation
class Dog(object):
def __init__(self, name, owner):
self.name = name
self.owner = owner
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name + “ says Woof!”)
class Cat(object):
def __init__(self, name, owner, lives=9):
self.name = name
self.owner = owner
self.lives = lives
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name + “ says Meow!”)
47
Current Implementation
class Dog(object):
def __init__(self, name, owner):
self.name = name
self.owner = owner
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name + “ says Woof!”)
class Cat(object):
def __init__(self, name, owner, lives=9):
self.name = name
self.owner = owner
self.lives = lives
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name + “ says Meow!”)
48
Any problems with this?
Current Implementation
class Dog(object):
def __init__(self, name, owner):
self.name = name
self.owner = owner
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name + “ says Woof!”)
class Cat(object):
def __init__(self, name, owner, lives=9):
self.name = name
self.owner = owner
self.lives = lives
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name + “ says Meow!”)
49
So much repeated code! :(
Current Implementation
class Dog(object):
def __init__(self, name, owner):
self.name = name
self.owner = owner
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name + “ says Woof!”)
class Cat(object):
def __init__(self, name, owner, lives=9):
self.name = name
self.owner = owner
self.lives = lives
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name + “ says Meow!”)
50
So much repeated code! :(
Let’s use inheritance!
51
Consolidate the similar code into one parent class that generalizes both subclasses!
Let’s use inheritance!
class Pet(object):
def __init__(self, name, owner):
self.is_alive = True
self.name = name
self.owner = owner
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name)
52
Consolidate the similar code into one parent class that generalizes both subclasses!
Let’s use inheritance!
class Pet(object):
def __init__(self, name, owner):
self.is_alive = True
self.name = name
self.owner = owner
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name)
53
Now we can have the Dog class extend the Pet class to inherit all of the instance variables and methods!
Syntax to do so:
class subclass(superclass)
Let’s use inheritance!
class Pet(object):
def __init__(self, name, owner):
self.is_alive = True
self.name = name
self.owner = owner
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name)
class Dog(Pet):
54
Now we can have the Dog class extend the Pet class to inherit all of the instance variables and methods!
Syntax to do so:
class subclass(superclass)
Let’s use inheritance!
class Pet(object):
def __init__(self, name, owner):
self.is_alive = True
self.name = name
self.owner = owner
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name)
class Dog(Pet):
55
If want a subclass to behave differently than parent class, override the method with a more specific implementation!
Let’s use inheritance!
class Pet(object):
def __init__(self, name, owner):
self.is_alive = True
self.name = name
self.owner = owner
def eat(self, thing):
print(self.name + “ate a ” + str(thing) + “!”)
def talk(self):
print(self.name)
class Dog(Pet):
def talk(self):
print(self.name + “ says Woof!”)
56
If want a subclass to behave differently than parent class, override the method with a more specific implementation!
Inheritance Hierarchy
57
Animal
Dog
Cat
Poodle
Inheritance Hierarchy
58
Animal
Dog
Cat
Poodle
class Poodle(Dog)
MUST follow an IS-A relationship