CS 61A Topical Review Object Oriented Programming Albert Xu Slides: - - PowerPoint PPT Presentation

cs 61a topical review
SMART_READER_LITE
LIVE PREVIEW

CS 61A Topical Review Object Oriented Programming Albert Xu Slides: - - PowerPoint PPT Presentation

CS 61A Topical Review Object Oriented Programming Albert Xu Slides: albertxu.xyz/teaching/cs61a/ OOP Formal Definitions Well need to know these words to write Dog Class - a template for an object represents attributes of a dog,


slide-1
SLIDE 1

CS 61A Topical Review

Object Oriented Programming

Slides: albertxu.xyz/teaching/cs61a/

Albert Xu

slide-2
SLIDE 2

OOP Formal Definitions

We’ll need to know these words to write Dog

  • Class - a “template” for an object
  • represents attributes of a dog, and the actions(methods) it can

take

slide-3
SLIDE 3

OOP Formal Definitions

We’ll need to know these words to write Dog

  • Class - a “template” for an object
  • represents attributes of a dog, and the actions(methods) it can

take

  • Instance - each object built using that template
  • a specific object, e.g. a specific dog!
slide-4
SLIDE 4

OOP Formal Definitions

We’ll need to know these words to write Dog

  • Class - a “template” for an object
  • represents attributes of a dog, and the actions(methods) it can

take

  • Instance - each object built using that template
  • a specific object, e.g. a specific dog!
  • Attributes
slide-5
SLIDE 5

OOP Formal Definitions

We’ll need to know these words to write Dog

  • Class - a “template” for an object
  • represents attributes of a dog, and the actions(methods) it can

take

  • Instance - each object built using that template
  • a specific object, e.g. a specific dog!
  • Attributes
  • instance
  • class
slide-6
SLIDE 6

OOP Formal Definitions

We’ll need to know these words to write Dog

  • Class - a “template” for an object
  • represents attributes of a dog, and the actions(methods) it can

take

  • Instance - each object built using that template
  • a specific object, e.g. a specific dog!
  • Attributes
  • instance
  • class
  • Method - a function bound to a particular class
  • an action a dog can take, e.g. “drink_water”
slide-7
SLIDE 7

On Inheritance

some things to remember By default, subclasses inherit their parent class’ attributes and methods

Dog Retriever Animal

Dog inherits from Animal superclass subclass

slide-8
SLIDE 8

On Inheritance

some things to remember By default, subclasses inherit their parent class’ attributes and methods Think about looking up attributes and methods of an object like looking up variable in environment diagrams! Check the object itself, then its parent object, then that object’s parent, etc.

Dog Retriever Animal

Animal inherits from Dog superclass subclass

slide-9
SLIDE 9

On Inheritance

some things to remember

By default, subclasses inherit their parent class attributes and methods. This means that they share attributes from the parent - they do not copy them!

Think about looking up attributes and methods of an object like looking up variable in environment diagrams! Check the object itself, then its parent object, then that object’s parent, etc.

Dog Animal

Animal inherits from Dog

Poodle Retriever

superclass subclass

slide-10
SLIDE 10

Object Retriever

Now write the Retriever object! 2) Does Retriever need a drink_water method? 1) What should Retriever’s constructor look like? (hint: how should we set color?)

We need to set the color attribute of itself to a specific color, “golden”! Because it’s in the subclass, this constructor will override the constructor inherited from the Dog class!

slide-11
SLIDE 11

Object Retriever

Now write the Retriever object! 2) Does Retriever need a drink_water method? 1) What should Retriever’s constructor look like? (hint: how should we set color?)

We need to set the color attribute of itself to a specific color, “golden”! Because it’s in the subclass, this constructor will override the constructor inherited from the Dog class!

Nope! Since we’re fine with the behavior of the original drink_water method, it’s automatically inherited from the parent class, Dog!

slide-12
SLIDE 12

Object Retriever

Now write the Retriever object!

class Retriever(Dog): def __init__(self); __________ = ________

2) Does Retriever need a drink_water method? 1) What should Retriever’s constructor look like? (hint: how should we set color?)

We need to set the color attribute of itself to a specific color, “golden”! Because it’s in the subclass, this constructor will override the constructor inherited from the Dog class!

Nope! Since we’re fine with the behavior of the original drink_water method, it’s automatically inherited from the parent class, Dog!

slide-13
SLIDE 13

Object Retriever

Now write the Retriever object!

class Retriever(Dog): def __init__(self); self.color = “golden”

2) Does Retriever need a drink_water method? 1) What should Retriever’s constructor look like? (hint: how should we set color?)

We need to set the color attribute of itself to a specific color, “golden”! Because it’s in the subclass, this constructor will override the constructor inherited from the Dog class!

Nope! Since we’re fine with the behavior of the original drink_water method, it’s automatically inherited from the parent class, Dog!

slide-14
SLIDE 14

Anatomy

slide-15
SLIDE 15

Anatomy

class header

slide-16
SLIDE 16

Anatomy

class header

class attributes

slide-17
SLIDE 17

Anatomy

class header

class attributes

constructor!

slide-18
SLIDE 18

Anatomy

class header

class attributes

constructor!

instance method

slide-19
SLIDE 19

Anatomy

class header

class attributes

constructor!

instance method

slide-20
SLIDE 20

Anatomy

class header

class attributes

constructor!

instance method

Two ways to call a method:

albert = Dog(“green”)

slide-21
SLIDE 21

Anatomy

class header

class attributes

constructor!

instance method

Two ways to call a method:

albert = Dog(“green”) Dog.drink_water(albert) unbound method call

slide-22
SLIDE 22

Anatomy

class header

class attributes

constructor!

instance method

Two ways to call a method:

albert = Dog(“green”) Dog.drink_water(albert) albert.drink_water() bound method call unbound method call

slide-23
SLIDE 23

*taken whole from a past CSM 61A MT2 Review

slide-24
SLIDE 24

Tricky WWPD

slide-25
SLIDE 25

Tricky WWPD

slide-26
SLIDE 26

Tricky WWPD

slide-27
SLIDE 27

Tricky WWPD

slide-28
SLIDE 28

Tricky WWPD

slide-29
SLIDE 29

Tricky WWPD

slide-30
SLIDE 30

Tricky WWPD

slide-31
SLIDE 31

Tricky WWPD

slide-32
SLIDE 32

Tricky WWPD

slide-33
SLIDE 33

A quick aside…

slide-34
SLIDE 34

A quick aside…

Why does this call even work? Shouldn’t we be passing in an instance?

slide-35
SLIDE 35

A quick aside…

Why does this call even work? Shouldn’t we be passing in an instance?

turns out Python doesn’t check whether self is actually an instance! You can pass in anything to an unbound

  • call. Of course, if you ever use self later on you’ll run

into issues….

slide-36
SLIDE 36

A quick aside…

Why does this call even work? Shouldn’t we be passing in an instance?

turns out Python doesn’t check whether self is actually an instance! You can pass in anything to an unbound

  • call. Of course, if you ever use self later on you’ll run

into issues….

Also, we can do this:

slide-37
SLIDE 37

A quick aside…

Why does this call even work? Shouldn’t we be passing in an instance?

turns out Python doesn’t check whether self is actually an instance! You can pass in anything to an unbound

  • call. Of course, if you ever use self later on you’ll run

into issues….

Also, we can do this:

slide-38
SLIDE 38

A quick aside…

Why does this call even work? Shouldn’t we be passing in an instance?

turns out Python doesn’t check whether self is actually an instance! You can pass in anything to an unbound

  • call. Of course, if you ever use self later on you’ll run

into issues….

Also, we can do this:

because self is actually just a convention in Python, not an enforced rule.

slide-39
SLIDE 39

Problem 1

slide-40
SLIDE 40

Problem 1

A new pet!

slide-41
SLIDE 41

Problem 1

A new pet! Error

slide-42
SLIDE 42

Problem 1

A new pet! Error Error

slide-43
SLIDE 43

Problem 1

A new pet! Error Error Quack

slide-44
SLIDE 44

Problem 1

A new pet! Error Error Quack A new pet!

slide-45
SLIDE 45

Problem 1

A new pet! Error Error Quack A new pet! ‘DaisyDaisy’

slide-46
SLIDE 46

Problem 1

A new pet! Error Error Quack A new pet! ‘DaisyDaisy’ What is wrong?

slide-47
SLIDE 47

Problem 1 Solution

A new pet! Error Error Quack A new pet! ‘DaisyDaisy’ What is wrong? None

slide-48
SLIDE 48

Problem 2

slide-49
SLIDE 49

Problem 2 Solution

“Sir, I work” Peon “Maam, I work”

Peon, I work “to gather wealth”

Comrade Peon, I work Peon

“Comrade Peon, I work”

slide-50
SLIDE 50

Thanks for coming.

Have a great rest of your week! :)

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/