1.5 Polymorphism

Introduction

When utilizing inheritance, sometimes we find ourselves in the situation where we have a function defined in the parent class, which we need to not only use in the child class, but also extend or modify. In order to accomplish this, we make use of Polymorphism.


Polymorphism

Wikipedia offers the following definition of Polymorphism:

"... the provision of a single interface to entities of different types." (Wikipedia)

Whilst technically correct, this is also potentially quite confusing. We can think of Polymorphism as the ability to use the same interface (or function call), where different implementations of that function will run dependent on the context in which the call was made.

As an example, suppose we were modeling animals. We might have a parent Animal class as follows.

Animal
---------------------
name
location
---------------------
talk()
move()
breathe()

Now, let's imagine we want to make use of three child classes which will be specializations of the parent class. For example, let's consider Bird, Dog, and Whale classes. Each of these classes will inherit from the Animal parent class, however, in each case, talk(), move() and breathe() will be done differently, even though they are all functions which animals perform. In this case, we would recreate the functions in the child classes with the exact same name and parameter list. This would then allow us to switch between child classes, and we could still use the same function calls.


Polymorphism in Python

Let's take a look at the code for the above example in Python:

class Animal:

    name = ""
    location = ""

    def __init__(self, name, location):
        self.name = name
        self.location = location

    def talk(self):
        pass

    def move(self):
        pass

    def breathe(self):
        pass

class Bird(Animal):
    def talk(self):
        print "Squawk"

    def move(self):
        print "Flying through the %s" % (self.location)

    def breathe(self):
        print "Take in air through beak"

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

    def move(self):
        print "Running on the %s" % (self.location)

    def breathe(self):
        print "Pant"

class Whale(Animal):
    def talk(self):
        print "Woooooeeeeeaaaowww"

    def move(self):
        print "Swimming in the %s" % (self.location)

    def breathe(self):
        print "Blow hole"

animals = [
    Bird("Polly", "sky"),
    Dog("Roger", "ground"),
    Whale("Moby", "sea")
]
for animal in animals:
    animal.talk()
    animal.move()
    animal.breathe()

As you can see, each of the specific animals classes makes use the three functions defined in the parent Animal class, but uses function overloading to change the implementation code of the function for the specific child class. This function overloading is what we mean when we refer to Polymorphism. Having said that, there are other forms of Polymorphism, other than function overloading. To read more about this, take a look at the wikipedia article.


Exercises

  1. Save the code in the above example in animals.py, run the code using the python command line interpreter and paste the output in the comments below.

Continue to next section


Comments

comments powered by Disqus