Object Oriented Programming

Breakout Exercise

Add a method for fleas. Does your dog or cat have fleas? Add a polymorphic method to the Animal example above for play -- Dog's fetch, and Cat's pounce.


In [ ]:
class Animal:
   def __init__(self, name=''):
      self.name = name
      self.has_fleas = False

   def talk(self):
      pass
    
   def play(self):
      pass

class Cat(Animal):
   def talk(self):
      print "Meow!"
        
   def play(self):
      print "Pounce!"


class Dog(Animal):
   def talk(self):
      print "Woof!"

   def play(self):
      print "Fetch!"

c = Cat("Missy")
c.talk()
print c.has_fleas

d = Dog("Rocky")
d.talk()
d.has_fleas=True
print d.has_fleas

In [ ]: