inheritanceexamples of inheritance ... summary of (single) inheritance multiple inheritance...

Download InheritanceExamples of inheritance ... Summary of (single) inheritance Multiple Inheritance Inheritance

If you can't read please download the document

Post on 06-Jun-2020

7 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • CSE 130 : Fall 2009

    Programming Languages

    Lecture 15: I h itInheritance

    Ranjit Jhala UC 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-class Polymorphism• 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 example class 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 here def 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 = 0 width = 0 def area(this):

    class Square: length = 0 def 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 = 0 width = 0 def area(this):

    class Square: length = 0 def 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 = 0 def area(this):( )

    return this.length * this.length

    class Rectangle(Square): width = 0width = 0 def 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 = 0 def 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 = 0 def area(this):

    return this.length * this.width

    method

    Option 2: Square is-a RectangleOption 2: Square is a Rectangle

    class Rectangle:class Rectangle: length = 0 width = 0 def area(this):( )

    return this.length * this.width

    class Square(Rectangle): init (self len):__init__(self,len): self.length = len self.width = len

  • Option 2: Square is-a RectangleOption 2: Square is a Rectangle

    class Rectangle: + Follows isa relationship class Rectangle: length = 0 width = 0 def area(this):

    + Follows isa relationship from math

    + Don’t need to write ( )return this.length * this.width

    + Don t need to write two area methods U t fi ld f

    class Square(Rectangle): init (self len):

    ―Use two fields for Square (len and width)

    __init__(self,len): self.length = len self.width = len

    Option 3: Option 3: class Shape:

    class Rectangle(Shape): class Square(Shape):

    ...

    g ( p ) length = 0 width = 0 def area(this):

    q ( p ) length = 0 def area(this):

    return this.length * return this.length *

    this.width this.length

    Option 3: Option 3: class Shape:

    class Rectangle(Shape): class Square(Shape):

    ...

    g ( p ) length = 0 width = 0 def area(this):

    q ( p ) length = 0 def area(this):

    return this.length * return this.length *

    this.width this.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 = 0 ComplexPart = 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

Recommended

View more >