subclasses & inheritance - cornell university · 2020. 12. 10. · announcements for today...
TRANSCRIPT
Subclasses &Inheritance
Presentation 19
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!
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
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
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?
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
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?
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Design Time: A (2D) Circle
11/5/20 Subclasses & Inheritance 28
Design Time: A (2D) Circle
11/5/20 Subclasses & Inheritance 29
What are the Attributes?
Design Time: A (2D) Circle
11/5/20 Subclasses & Inheritance 30
What are the Methods?
Design Time: A (2D) Circle
11/5/20 Subclasses & Inheritance 31
What are the Invariants?
Design Time: A (2D) Circle
11/5/20 Subclasses & Inheritance 32
Ellipse
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
Questions?
11/5/20 Subclasses & Inheritance 34