Post on 06-Jun-2020
CSE 130 : Fall 2009
Programming Languages
Lecture 15: I h itInheritance
Ranjit JhalaUC San Diego
InheritanceInheritance
• Key concept of OO languages• Key concept of OO languages
S ll h i h i i ?• Someone tell me what inheritance is?
InheritanceInheritance
• Key concept of OO languages• Key concept of OO languages
S ll h i h i i ?• Someone tell me what inheritance is?• “is-a” relationship
• Examples?• Examples?
Examples of inheritanceExamples of inheritance
OverridingOverriding
• Super-class method can be overwritten in • Super class method can be overwritten in sub-classPolymorphism• Polymorphism– external clients write code that handles
different kinds of objects in uniform waydifferent kinds of objects in uniform way
d ’t b t i l t ti d t il – don’t care about implementation details: as long as the object knows to draw itself, that’s good enough (see screensaver on webpage)good enough (see screensaver on webpage)
Polymorphism continuedPolymorphism, continued
• Super-class can have methods that are • Super class can have methods that are not overridden, but that work differently for different sub-classesfor different sub classes
F l l th d • For example: super-class method functionality changes because the super-l ll th d th t t itt class calls a method that gets overwritten
in the sub-class
Simple exampleSimple exampleclass Shape:
def draw(self, screen):# some python code here
def erase(self, screen):screen.setcolor(“white”)screen.setcolor( white )self.draw(screen)screen.setcolor(“black”)
class Rec(Shape):def draw(self screen):
class Oval(Shape):def draw(self screen):def draw(self, screen):
# some python code heredef draw(self, screen):
# some python code here
A QuestionA Question
How can “Oval”’s draw method call the How can Oval s draw method call the superclass’s draw method ?
Stepping away from PythonStepping away from Python
• What are the fundamental issues with • What are the fundamental issues with inheritance?
• How to decide on the inheritance graph?– not always obvious, see next example
Rectangle and SquareRectangle and Square
l R t l l Sclass Rectangle:length = 0width = 0def area(this):
class Square:length = 0def area(this):
return this length *def area(this):return this.length *
this.width
return this.length *this.length
• Which should be a sub-class of which?
Rectangle and SquareRectangle and Square
l R t l l Sclass Rectangle:length = 0width = 0def area(this):
class Square:length = 0def area(this):
return this length *def area(this):return this.length *
this.width
return this.length *this.length
• Which should be a sub-class of which?
• Answer is not clear...
Option 1: Rectangle is-a SquareOption 1: Rectangle is a Square
class Square:length = 0def area(this):( )
return this.length *this.length
class Rectangle(Square):width = 0width = 0def area(this):
return this.length *this.width
Option 1: Rectangle is-a SquareOption 1: Rectangle is a Square
+ Store only what is + Store only what is needed (one field for square)
class Square:length = 0def area(this): square)
―Does not follow “isa” relationship from math
( )return this.length *
this.length
relationship from math (rectangle is not a square )
class Rectangle(Square):width = 0 square...)
―Have to override area th d
width = 0def area(this):
return this.length *this.width
method
Option 2: Square is-a RectangleOption 2: Square is a Rectangle
class Rectangle:class Rectangle:length = 0width = 0def area(this):( )
return this.length *this.width
class Square(Rectangle):init (self len):__init__(self,len):self.length = lenself.width = len
Option 2: Square is-a RectangleOption 2: Square is a Rectangle
class Rectangle: + Follows isa relationship class Rectangle:length = 0width = 0def area(this):
+ Follows isa relationship from math
+ Don’t need to write ( )return this.length *
this.width
+ Don t need to write two area methodsU t fi ld f
class Square(Rectangle):init (self len):
―Use two fields for Square (len and width)
__init__(self,len):self.length = lenself.width = len
Option 3: Option 3: class Shape:
class Rectangle(Shape): class Square(Shape):
...
g ( p )length = 0width = 0def area(this):
q ( p )length = 0def area(this):
return this.length *return this.length *
this.widththis.length
Option 3: Option 3: class Shape:
class Rectangle(Shape): class Square(Shape):
...
g ( p )length = 0width = 0def area(this):
q ( p )length = 0def area(this):
return this.length *return this.length *
this.widththis.length
St l h t i d d ( fi ld f )+ Store only what is needed (one field for square)― Does not follow “isa” relationship from math
(rectangle is not a square )(rectangle is not a square...)― Have to write two area methods
Complex numbersComplex numbers
l R l l C lclass Real:RealPart = 0
class Complex:RealPart = 0ComplexPart = 0
The same exact options present themselves here, with the same tradeoffs!with the same tradeoffs!
Summary of (single) inheritanceSummary of (single) inheritance
• Inheritance is a powerful mechanismInheritance is a powerful mechanism
• For the programmer • For the programmer, difficulty = defining inheritance diagram
• For the language implementer, difficulty = fast dynamic dispatch
Multiple InheritanceMultiple Inheritance
Next timeNext time
• Multiple inheritance• Multiple inheritance
D• Decorators