subclasses & inheritance - cornell university · 2020. 12. 10. · announcements for today...

34
Subclasses & Inheritance Presentation 19

Upload: others

Post on 31-Dec-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Subclasses &Inheritance

Presentation 19

Page 2: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Announcements for Today

Assignments Video Lessons

• Lesson 22 for today • Videos 23.1-23.7 next time• Video 23.8 is very optional

11/5/20 Subclasses & Inheritance 2

• A4 graded by next week§ Last call for the Survey

• A5 was posted Tuesday§ Shorter written assignment§ Due Tuesday at Midnight

• A6 also posted Tuesday§ Due Sunday after classes§ Designed to take two weeks§ Follow micro-deadlines!

Page 3: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Second Prelim is Coming!

• We are now two weeks out!§ Thursday November 19th at 9:30 am (Eastern)§ Will cover up to November 12th

§ Topics: Loops, Recursion, and Classes• CMS is now open for conflicts again

§ Time zone conflicts will be resolved later§ Submit if you want/need to move online§ SDS students should submit again§ Want to assign groups by next Thurday

11/5/20 Subclasses & Inheritance 3

Page 4: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Second Prelim is Coming!

• We are now two weeks out!§ Thursday November 19th at 9:30 am (Eastern)§ Will cover up to November 12th

§ Topics: Loops, Recursion, and Classes• CMS is now open for conflicts again

§ Time zone conflicts will be resolved later§ Submit if you want/need to move online§ SDS students should submit again§ Want to assign groups by next Thursday

11/5/20 Subclasses & Inheritance 4

Study Guide posted next week

Page 5: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Subclass Constructors

class A(object):

def __init__(self,x):self.x = x self.y = x

class B(A):

def __init__(self,y):super().__init__(y+2)self.x = y

>>> b = B(3)

11/5/20 Subclasses & Inheritance 5

1312

14

202122

What does the heaplook like now?

Ignoring the class folderwhat does the call stack and the heap look like?

Page 6: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

B:

C: D:

A:

Which One is Closest to Your Answer?

11/5/20 Subclasses & Inheritance 6

id1B

id1A

id1A

A.__init__ 13

id1self 3x

B.__init__ 21

id1self 3yid1

B

Page 7: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Subclass Constructors

class A(object):

def __init__(self,x):self.x = x self.y = x

class B(A):

def __init__(self,y):super().__init__(y+2)self.x = y

>>> b = B(3)

11/5/20 Subclasses & Inheritance 7

1312

14

202122

C:id1

B

What is the next step?

Page 8: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

B:

C: D:

A:

Which One is Closest to Your Answer?

11/5/20 Subclasses & Inheritance 8

id1B

3x

id1B

id1B

id1B

A.__init__ 13

id1self 3x

B.__init__ 21

id1self 3y

B.__init__ 21

3y

A.__init__ 13

3xid1self id1self

3x

Page 9: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Subclass Constructors

class A(object):

def __init__(self,x):self.x = x self.y = x

class B(A):

def __init__(self,y):super().__init__(y+2)self.x = y

>>> b = B(3)

11/5/20 Subclasses & Inheritance 9

1312

14

202122 What is the next step?

C:id1

B

B.__init__ 21

3yid1self

Page 10: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

B:

C: D:

A:

Which One is Closest to Your Answer?

11/5/20 Subclasses & Inheritance 10

id1B

3x

id1B

id1B

id1B

B.__init__ 21id1self 3yB.__init__ 21

id1self 3y5x

A.__init__ 13id1self 5x

B.__init__ 21id1self 3y

A.__init__ 13id1self 3x

B.__init__ 21id1self 3y

A.__init__ 13id1self 5x

Page 11: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Subclass Constructors

class A(object):

def __init__(self,x):self.x = x self.y = x

class B(A):

def __init__(self,y):super().__init__(y+2)self.x = y

>>> b = B(3)

11/5/20 Subclasses & Inheritance 11

1312

14

202122 What is the next step?

C:id1

B

B.__init__ 21id1self 3y

A.__init__ 13id1self 5x

Page 12: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

B:

C: D:

A:

Which One is Closest to Your Answer?

11/5/20 Subclasses & Inheritance 12

id1B

5x

id1B

id1B

id1B

B.__init__ 21id1self 3y

5xA.__init__

id1self 5x

B.__init__ 21id1self 3y

A.__init__ 14id1self 5x

B.__init__ 21id1self 3y

A.__init__ 14id1self 5x

B.__init__ 21id1self 3y

A.__init__ 13id1self 5x

5x 3x

Page 13: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Subclass Constructors

class A(object):

def __init__(self,x):self.x = x self.y = x

class B(A):

def __init__(self,y):super().__init__(y+2)self.x = y

>>> b = B(3)

11/5/20 Subclasses & Inheritance 13

1312

14

202122 What is the next step?

D:id1

B

B.__init__ 21id1self 3y

A.__init__ 14id1self 5x

5x

Page 14: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

B:

C: D:

A:

Which One is Closest to Your Answer?

11/5/20 Subclasses & Inheritance 14

id1B

5x

id1B

id1B

id1B

B.__init__ 13id1self 3x

5xA.__init__

id1self 5x

B.__init__ 21id1self 3x

A.__init__ 14id1self 5x

B.__init__ 21id1self 3y

A.__init__ 15id1self 5x

B.__init__ 21id1self 3y

A.__init__id1self 5x

5x 5x5y

5y5y

5y

Page 15: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Subclass Constructors

class A(object):

def __init__(self,x):self.x = x self.y = x

class B(A):

def __init__(self,y):super().__init__(y+2)self.x = y

>>> b = B(3)

11/5/20 Subclasses & Inheritance 15

1312

14

202122 What is the next step?

A:

id1B

5x

B.__init__ 21id1self 3y

A.__init__id1self 5x5y

Page 16: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

B:

C: D:

A:

Which One is Closest to Your Answer?

11/5/20 Subclasses & Inheritance 16

id1B

5x

id1B

id1B

id1B

B.__init__ 22id1self 3x

5xA.__init__

id1self 5x

B.__init__ 22id1self 3x

A.__init__id1self 5x

B.__init__ 22id1self 3x

B.__init__ 21id1self 3x

A.__init__id1self 5x

5x 5 3x5y

5y5y

5yx

Page 17: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Subclass Constructors

class A(object):

def __init__(self,x):self.x = x self.y = x

class B(A):

def __init__(self,y):super().__init__(y+2)self.x = y

>>> b = B(3)

11/5/20 Subclasses & Inheritance 17

1312

14

202122 What is the next step?

B:id1

B

A.__init__ 22id1self 3y

5xB.__init__

id1self 5x5y

Page 18: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

B:

C: D:

A:

Which One is Closest to Your Answer?

11/5/20 Subclasses & Inheritance 18

id1B

id1B

id1B

id1B

B.__init__

id1self 3y

B.__init__

id1self 3y

B.__init__

3y

B.__init__

3yid1self id1self

5y

5y5y

5y

5x

5 3x

5 3x

5 3x

id1RETURNx3x

x x

Page 19: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Subclass Constructors

class A(object):

def __init__(self,x):super().__init__(x+2)self.x = x

class B(A):

def __init__(self,y):self.x = yself.y = y

>>> b = B(3)

11/5/20 Subclasses & Inheritance 19

1312

14

202122 Final step is erase frame

C:id1

B

B.__init__

3yid1self

5y5 3x x

Page 20: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Name Resolutionclass A(object):

x = 3 # Class Attribute y = 5 # Class Attributedef f(self):

return self.g()def g(self):

return 10

class B(A):y = 4 # Class Attribute z = 42 # Class Attribute def g(self):

return 14def h(self):

return 18

• Execute the following:>>> a = A()>>> b = B()

• What is value of a.f()?

11/5/20 Subclasses & Inheritance 20

A: 10B: 14C: 5D: ERRORE: I don’t know

Page 21: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Name Resolutionclass A(object):

x = 3 # Class Attribute y = 5 # Class Attributedef f(self):

return self.g()def g(self):

return 10

class B(A):y = 4 # Class Attribute z = 42 # Class Attribute def g(self):

return 14def h(self):

return 18

• Execute the following:>>> a = A()>>> b = B()

• What is value of a.f()?

11/5/20 Subclasses & Inheritance 21

A: 10B: 14C: 5D: ERRORE: I don’t know

CORRECT

Page 22: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Name Resolutionclass A(object):

x = 3 # Class Attribute y = 5 # Class Attributedef f(self):

return self.g()def g(self):

return 10

class B(A):y = 4 # Class Attribute z = 42 # Class Attribute def g(self):

return 14def h(self):

return 18

• Execute the following:>>> a = A()>>> b = B()

• What is value of b.f()?

11/5/20 Subclasses & Inheritance 22

A: 10B: 14C: 5D: ERRORE: I don’t know

Page 23: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Name Resolutionclass A(object):

x = 3 # Class Attribute y = 5 # Class Attributedef f(self):

return self.g()def g(self):

return 10

class B(A):y = 4 # Class Attribute z = 42 # Class Attribute def g(self):

return 14def h(self):

return 18

• Execute the following:>>> a = A()>>> b = B()

• What is value of b.f()?

11/5/20 Subclasses & Inheritance 23

A: 10B: 14C: 5D: ERRORE: I don’t know

CORRECT

Page 24: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Name Resolutionclass A(object):

x = 3 # Class Attribute y = 5 # Class Attributedef f(self):

return self.g()def g(self):

return 10

class B(A):y = 4 # Class Attribute z = 42 # Class Attribute def g(self):

return 14def h(self):

return 18

• Execute the following:>>> a = A()>>> b = B()

• What is value of b.x?

11/5/20 Subclasses & Inheritance 24

A: 4B: 3C: 42D: ERRORE: I don’t know

Page 25: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Name Resolutionclass A(object):

x = 3 # Class Attribute y = 5 # Class Attributedef f(self):

return self.g()def g(self):

return 10

class B(A):y = 4 # Class Attribute z = 42 # Class Attribute def g(self):

return 14def h(self):

return 18

• Execute the following:>>> a = A()>>> b = B()

• What is value of b.x?

11/5/20 Subclasses & Inheritance 25

A: 4B: 3C: 42D: ERRORE: I don’t know

CORRECT

Page 26: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Name Resolutionclass A(object):

x = 3 # Class Attribute y = 5 # Class Attributedef f(self):

return self.g()def g(self):

return 10

class B(A):y = 4 # Class Attribute z = 42 # Class Attribute def g(self):

return 14def h(self):

return 18

• Execute the following:>>> a = A()>>> b = B()

• What is value of a.z?

11/5/20 Subclasses & Inheritance 26

A: 4B: 3C: 42D: ERRORE: I don’t know

Page 27: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Name Resolutionclass A(object):

x = 3 # Class Attribute y = 5 # Class Attributedef f(self):

return self.g()def g(self):

return 10

class B(A):y = 4 # Class Attribute z = 42 # Class Attribute def g(self):

return 14def h(self):

return 18

• Execute the following:>>> a = A()>>> b = B()

• What is value of a.z?

11/5/20 Subclasses & Inheritance 27

A: 4B: 3C: 42D: ERRORE: I don’t know

CORRECT

Page 28: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Design Time: A (2D) Circle

11/5/20 Subclasses & Inheritance 28

Page 29: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Design Time: A (2D) Circle

11/5/20 Subclasses & Inheritance 29

What are the Attributes?

Page 30: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Design Time: A (2D) Circle

11/5/20 Subclasses & Inheritance 30

What are the Methods?

Page 31: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Design Time: A (2D) Circle

11/5/20 Subclasses & Inheritance 31

What are the Invariants?

Page 32: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Design Time: A (2D) Circle

11/5/20 Subclasses & Inheritance 32

Ellipse

Page 33: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Design Time: A (2D) Circle

11/5/20 Subclasses & Inheritance 33

Ellipse

How to use subclassing?A: Circle parent, Ellipse subclass B: Circle subclass, Ellipse parent C: Either can be the subclassD: Subclassing is not useful hereE: I do not know

Page 34: Subclasses & Inheritance - Cornell University · 2020. 12. 10. · Announcements for Today Assignments Video Lessons •Lesson 22 for today •Videos 23.1-23.7 next time •Video

Questions?

11/5/20 Subclasses & Inheritance 34