Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the...

32
Class Inheritance Victor Norman CS104

Transcript of Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the...

Page 1: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Class Inheritance

Victor NormanCS104

Page 2: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Reading Quiz, Q1

In the first reading, the author uses the following classes to illustrate Subclassing:

A. Shape, Circle, LineB. Pet, Dog, CatC. Furniture, Table, ChairD. Beam, Ibeam, Hbeam

Page 3: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Reading Quiz, Q2

If you have defined the class Dog to be a subclass of class Pet and have created a variable dog this way:dog = Dog(“Spot”, True)then the following calls isinstance(dog, Pet)isinstance(dog, Dog)will return…A. True, TrueB. False, TrueC. True, FalseD. False, False

Page 4: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Reading Quiz, Q3

If we have a class Vehicle with a method status() defined in it, and a class Motorcycle that is a subclass of Vehicle, then we can create a Motorcycle instance and call the status() method on it because…A. Motorcycle uses Vehicle.B. Motorcycle initializes Vehicle.C. Motorcycle inherits status() from Vehicle.D. Motorcycle overrides status() in Vehicle.

Page 5: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

The Problem

What is the basic problem that inheritance tries to solve?

Answer:Two types (classes) may have lots of similar code. Inheritance is a way to reuse code, thus removing duplicate code.

Page 6: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Drawing Man and Womanclass Man: def __init__(self, x, y): self._x = x self._y = y def draw(self): self.drawHead() self.drawBody() self.drawArms() self.drawLegs() def drawHead(self): <code here> # code for drawArms # code for drawBody def drawLegs(self): <code to draw legs>

class Woman: def __init__(self, x, y): self._x = x self._y = y def draw(self): self.drawHead() self.drawBody() self.drawArms() self.drawLegs() def drawHead(self): <code here> # code for drawArms # code for drawBody def drawLegs(self): <code to draw skirt>

Page 7: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Lots of duplicate code

• So what? Who cares?

• Duplicate code – more places to make errors– more places to fix errors– inconsistent fixes– more typing– more places to add new code

Page 8: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Solution: a Person class instead of Man/Woman

class Person: def __init__(self, x, y, gen): self._x = x self._y = y self._gender = gen def draw(self): self.drawHead() self.drawBody() self.drawArms() self.drawLegs() # drawHead, Body, Arms code… def drawLegs(self): if self._gender == "F": self.drawSkirtAndLegs()

else: self.drawHairyLegs()

def drawHairyLegs(self): <code> def drawSkirtAndLegs(self): <code>

class Man: def __init__(self, x, y): self._x = x self._y = y def draw(self): self.drawHead() self.drawBody() self.drawArms() self.drawLegs() # drawHead, Body, Arms code… def drawLegs(self): <code to draw legs>

Page 9: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Evaluate this solution

+ Much less code in Person than in Man + Woman.+ Only 1 class. (Less is almost always better.)− Does not scale well:Consider: adding Alien now: 3 legs, that bend backwards.def drawLegs(self): if self._gender == “F”: self.drawSkirtAndLegs() elif self._gender == “M”: self.drawHairyLegs() elif self._gender == “A”: self.drawAlienLegs()

If we decide to drawHeads differently we need a big if-elif-elif again… and same for drawBody()…

Page 10: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Solution: class inheritance

• Keep Person, and make Man, Woman, and Alien be subclasses

Person

Man Woman Alien

Page 11: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Terminology

• Person is the superclass, or base class, or parent class.• Man, Woman, Alien are subclasses, derived classes, or

child classes.• Subclasses inherit from or are derived from

superclasses.

• Want all common attributes and methods/code “pushed” up the hierarchy.– e.g., Man, Woman, Alien all have x, y location: should be

stored in Person superclass.

Page 12: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Person Class Codeclass Person: # an abstract base class. def __init__(self, x_loc, y_loc, gender): self._x = x_loc self._y = y_loc self._gender = gender def draw(self): self.drawHead() self.drawBody() self.drawArms() self.drawLegs() # def drawHead, drawBody, drawArms, etc. # NOTE: No drawLegs() code here. Only in # derived classes. Also, don’t really need # to store gender anymore.

Page 13: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Manclass Man(Person): def __init__(self, x_loc, y_loc): Person.__init__(self, x_loc, y_loc, "M")

def drawLegs(self): # code to draw hairy bowlegged legs wearing jeans.

“A Man is-a Person”

Call superclass constructor, passing in

reference to self, location and gender

drawLegs() implemented here – not

in superclass

Page 14: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Woman, Alienfrom person import * # assuming Person is in person.py

class Woman(Person): def __init__(self, x_loc, y_loc): Person.__init__(self, x_loc, y_loc, “F”)

def drawLegs(self): # code to draw legs under a nice modest skirt.

class Alien(Person): def __init__(self, x_loc, y_loc): Person.__init__(self, x_loc, y_loc, "A")

def drawLegs(self): # code to draw 3 legs.

Page 15: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Using this code

barack = Man(10, 20)beyonce = Woman(30, 40)e_t = Alien(50, 60)

beings = [ barack, beyonce, e_t ]for be in beings: be.draw()

Page 16: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

myro.py details

• myro library provides move(), motors(), turnLeft(), turnRight(), setLEDs(), beep(), etc.– When you call move(), you are actually calling • robot.move(): robot is a pre-defined global object.

• Also, provides a class definition:class Scribbler: def __init__(self): … def init(self, comport=None): … def turnLeft(self, time): … def beep(self, time, freq1, freq2=None): …

Page 17: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

What we did last week

• We defined ScribPoint– attributes: _x, _y– getX(), getY(), setX(), setY()– Just held the coordinates. Not much fcnality.

• We defined goToPoint(from, to):– from and to were ScribPoint objects.– used our turnDegrees(), which called

turnLeftDegrees() or turnRightDegrees(), which used globally pre-defined robot object.

Page 18: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Analysis of this solution

• ScribPoint has very little functionality.• We had to make the robot turn east after

moving to a new point.– More turns more inconsistent results.

• Had to pass in to goToPoint() where the robot was.

Page 19: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Better way

• Wouldn’t it be loverly if – the robot could “remember” where it is and what

angle it is pointing at.• But, how to do this? Two options:– Have a class that “has-a” a reference to a Scribbler

object and keeps track of this stuff, or,– Have a class that “is-a” Scribbler object, adding

the functionality and attributes.• Latter is Class Inheritance.

Page 20: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

CS104Scribbler

• A CS104Scribbler “is-a” Scribbler.– inherits from it.

from myro import * # imports Scribbler defn.class CS104Scribbler(Scribbler): def __init__(self, comport=“COM40”, x=0, y=0, angle=0): Scribbler.__init__(self, comport) # code to store x, y, angle.

Page 21: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Result

• We can move goToPoint(), turnDegrees(), etc., into CS104Scribbler class.

• Can write this nice code:scrib = CS104Scribbler() # uses COM40scrib.goToPoint(16, 12)scrib.goToPoint(0, 0)

• Nice and compact and readable.• goToPoint() does not have to “turn east” after

each move, because object can remember what angle it is pointing.

Page 22: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Additional Benefits

• We could write code to control 2 robots:

scrib1 = CS104Scribbler(“COM40”)scrib2 = CS104Scribbler(“COM41”)# send different messages to robotsscrib1.beep(3, 660, 770)scrib2.beep(3, 440, 487)

Page 23: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Extra Slides…

Page 24: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

New class or child class?

Q: If a class needs a couple attributes belonging to another class, is it better to make a child class or a completely new class?

A: If it is just attributes, just make a new class. If it is attributes and a bunch of functionality, make a subclass.

Page 25: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

When to subclass?

Q: When would it be better to use the Parent and Child class rather than defining methods in one class?A: 1) You might need to be able to make objects of both kinds of classes. 2) You might have the Parent class well tested – so you don’t want to alter it. 3) You anticipate that you might need another child class of the parent.

Page 26: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

SortedSet

Q: I am wondering why a whole separate class is created for some of these functions. For stuff like SortedSet, the case can be made, but I feel like we can accomplish the same thing with a “plain old list” and functions contained in that class.A: I agree. The example is a bit contrived. I would have started from scratch with a class containing a list.

Page 27: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

SortedSet (2)

Q: So the "sort" method is created just to prevent people from sorting the already sorted set? And why wouldn't we want to reverse it?A: Right. sort’s code consists of pass. This is done so that it is efficient, instead of trying to sort an already sorted list. You don’t want to reverse it because then it is not sorted anymore.NOTE: when you find you are “undefining” methods provided by the superclass, this should be an indication that you’ve made a poor inheritance decision.

Page 28: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

How to see defined methods?

Q: Is there a way to see all the preexisting methods for a parent class?

A: Best way is to do >>> help(class)

in interactive mode.

Page 29: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

What does this do?

Q: Are we modifying the class Television or the class DeluxeTV with this def?class DeluxeTV(Television): def __init__(self): Television.__init__(self) self._favorites = [ ]

A: You are modifying the DeluxeTV class – adding _favorites to it.

Page 30: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

__setitem__

Q: What is going on with the __setitem__

method?A: __setitem__ is called when you do indexing: __setitem__(...)

x.__setitem__(i, y) <==> x[i]=y

Page 31: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

Exampleclass Vic: def __init__(self): self._l = [] def __setitem__(self, loc, item): if len(self._l) > loc: self._l[loc] = item else: numItemsToAdd = loc - len(self._l) for i in range( numItemsToAdd): self._l.append(0) self._l.append(item)

v = Vic()v[0] = "hi"v[3] = "hello"print v._lOUTPUT: ['hi', 0, 0, 'hello']

Page 32: Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,

“Inner” classes

Q: Is it possible to have a class in a class?A: It is possible, but I’ve never had to do it.