polymorphism Inheritance Base class Derived class
recall from last class -polymorphism r = a rectangle t = a triangle c = a circle txt = some text shapeList=[r,t,c,txt] for shape in shapeList: shape.draw(win)
Inheritance mammal, horse, kangaroo mammal horse kangaroo
Inheritance mammal, horse, kangaroo mammal backbone fur mammal horse 4 legs tail kangaroo backbone fur pocket tail horse kangaroo
Inheritance Inheritance :derive a new class from an existing one existing class:the parent class, or superclass, or base class The new class: derived class ,the child class or subclass the child inherits characteristics of the parent That is, the child class inherits the methods and data defined by the parent class Copyright © 2012 Pearson Education, Inc.
is-a relationship get all the member variables get all the member methods if you want. can add more variables can add or modify methods software reuse is good – already tested, etc.
Base class: Animal class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, unchanged, for Duck. def eat(self): print("Eat, eat, eat.")
Base class: Animal - constructor class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies Has instance data self.name (a string) self.flies(boolean, default is False)
Base class: Animal – other methods def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, # unchanged, for Duck. def eat(self): print("Eat, eat, eat.")
Derived class: Duck class Duck(Animal): #The derived constructor calls # the base constructor, # then does more def __init__(self, feathers): Animal.__init__(self,"duck") self.flies=True self.feathers=feathers
Derived class: Duck - constructor class Duck(Animal): #The derived constructor calls # the base constructor, # then does more def __init__(self, feathers): Animal.__init__(self,"duck") self.flies=True self.feathers=feathers instance data self.name (from Animal) self.flies (from Animal) self.feathers (just for Duck)
Derived class: Duck – other methods #This display does more than the animal display, #but makes use of Animal.display def display(self): print("I am a", end=" ") Animal.display(self) print(" with ",self.feathers, " feathers") #This talk totally overrides the Animal talk def talk(self): print("Quack! Quack!") # Animal has no move method. Only for Ducks. def move(self): print("I swim. I fly. I walk")
main def main(): a=Animal() a.display() print() a.talk() a.eat() d=Duck(1000) d.display() d.talk() d.eat() d.move()
class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, unchanged, for Duck. def eat(self): print("Eat, eat, eat.")
main def main(): a=Animal() a.display() print() a.talk() a.eat() d=Duck(1000) d.display() d.talk() d.eat() d.move() just an animal self.name False self.flies a just an animal Indistinct animal noises Eat, eat, eat.
class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, unchanged, for Duck. def eat(self): print("Eat, eat, eat.")
class Duck(Animal): #The derived constructor calls the base constructor, #then does more def __init__(self, feathers): Animal.__init__(self,"duck") self.flies=True self.feathers=feathers #This display does more than the animal display, #but makes use of Animal.display def display(self): print("I am a", end=" ") Animal.display(self) print(" with ",self.feathers, " feathers") #This talk total overrides the Animal talk def talk(self): print("Quack! Quack!") # Animal has no move method. Only for Ducks. def move(self): print("I swim. I fly. I walk")
main just an animal Indistinct animal noises Eat, eat, eat. I am a duck with 1000 feathers Quack! Quack! I swim. I fly. I walk def main(): a=Animal() a.display() print() a.talk() a.eat() d=Duck(1000) d.display() d.talk() d.eat() d.move() a just an animal self.name False self.flies 1000 self.feathers duck self.name False True self.flies d