1.6 Abstraction

Introduction

When designing classes which make use of inheritance, you might not want to allow the creation of an object instance of the parent class. In other words, you might want to force the use of a child class rather than the parent class. For this, we specify the parent class as being Abstract, meaning that it cannot be instantiated as an object. So, to create an object, one of the child classes needs to be used.


Abstraction

Let's examine, again, the animals example from the previous section.

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

Bird(Animal)

Dog(Animal)

Whale(Animal)

We would not want to allow an object of type Animal to be created. We would rather want an object of type Bird, or Dog, or Whale to be created instead. Thus, we define the Animal class as Abstract, leaving Bird, Dog and Whale as Concrete classes which can be instantiated as objects.


Abstraction in Python

Abstraction in Python has only been recently introduced by means of the abs (Abstract Base Classes) class. The syntax is as follows.

from abc import ABCMeta, abstractmethod

class MyAbstractClass:  
    __metaclass__ = ABCMeta

    @abstractmethod
    def getById(self, userId):  
        pass

Here, there are three important features of the code to take note of:

  1. from abc import... : This line imports functionality from a Python module into the current script. To learn more about Python modules and importing, take a look at this article. The abc module provides the functionality we need to make this class Abstract.
  2. __metaclass__ = ABCMeta : This line defines the class as a whole as Abstract.
  3. @abstractmethod : This is referred to as a decorator, which is used to modify a function. This Abstract Method decorator is used to define a function as Abstract.

Let's apply this now to our Animal class to make it abstract.

from abc import ABCMeta, abstractmethod

class Animal:
    __metaclass__ = ABCMeta

    name = ""
    location = ""

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

    @abstractmethod
    def talk():
        pass

    @abstractmethod
    def move():
        pass

    @abstractmethod
    def breathe():
        pass

Now, if we were to attempt to instantiate an object of the Animal class, we would get a TypeError. Let's confirm this:

a = Animal("Something", "Somewhere")
    TypeError: Can't instantiate abstract class Animal with abstract methods talk

Exercises

  1. Rewrite the animal.py file from the previous section, apply abstraction to the Animal parent class, run the file through the Python interpreter and check that your code still functions as expected.
  2. Write a program which defines an abstract class, "Contact", and two specialized child classes "IndividualContact" and "CompanyContact". Paste the class definitions in a comment below for feedback.

Continue to the next section.


Comments

comments powered by Disqus