Python Inheritance

Where a program is being created, that incorporates a number of Classes, which contain common Attributes and Methods, inheritance can be used so that there is no code duplication. A Class, sometimes referred to as a Base Class, can be created, which contains the common Attributes and Methods. Other Classes can then be created, which inherit these Attributes and Methods, as well as provide additional, more specific Attributes and Methods. This is illustrated below, using animals as an example.

All animals, regardless of what they are, have common Attributes, such as name and age. They also perform common actions, such as eating. The ‘Animal’ Class below contains these common Attributes and actions or Methods. The ‘Dog’ and ‘Cat’ Classes inherit these Attributes and Methods from the ‘Animal’ Class and extend this further by adding specific Methods. When an instance of the ‘Dog’ or ‘Cat’ Class is created, the Attributes and Methods from the ‘Animal’ Class can be referred to as though they were defined directly in these Classes.

class Animal:

    def __init__(self, name, age=0):
        self.name = name
        self.age = age

    def eat(self, food):
        print('{} is eating {}.'.format(self.name, food))

    def yearsold(self):
        if self.age == 0:
            print('{} is of an unknown age.'.format(self.name))
        elif self.age == 1:
            print('{} is {} year old.'.format(self.name, self.age))
        else:
            print('{} is {} years old.'.format(self.name, self.age))


class Dog(Animal):

    def fetch(self, thing):
        print('{} goes after the {}!'.format(self.name, thing))


class Cat(Animal):

    def swatstring(self):
        print('{} shreds the string!'.format(self.name))

The first Method in the ‘Animal’ Class, ‘__init__’, is a special type of Method that is automatically executed when an instance of the Class is created, which in this case, initialises two Attributes of the Class, ‘name’ and ‘age’. As well as the standard ‘self’ parameter, ‘name’ and ‘age’ are parameters passed in to this Method, although ‘age’ is optional and is initialised to zero if it isn’t provided. The ‘eat’ Method requires an additional parameter of ‘food’, whilst the ‘yearsold’ Method requires no additional parameters, other than ‘self’.

The ‘Dog’ and ‘Cat’ Classes, as well as inheriting these Attributes and Methods from ‘Animal’, add an extra Method each, ‘fetch’ for the ‘Dog’ Class and ‘swatstring’ for the ‘Cat’ Class.

Below are examples of how the ‘Dog’ and ‘Cat’ Classes can be instantiated.

dog = Dog('Bobby', 5)
cat = Cat('Betty')

Notice that both the name and age have been provided as parameters in the case of the dog, however, only the name has been provided for the cat, so its age will be initialised to zero.

In order to call the ‘eat’ Method, one parameter has to be provided, the food being eaten.

dog.eat('dog food')
cat.eat('cat food')

The output from these two calls to the ‘eat’ Method is as follows, which incorporates the ‘name’ Attribute previously set.

Bobby is eating dog food.
Betty is eating cat food.

Calling the ‘yearsold’ Method can be done in the same manner, except that no additional parameters need to be passed.

dog.yearsold()
cat.yearsold()

Again, the name is incorporated into the output. As the age wasn’t set for the cat, this is shown as unknown.

Bobby is 5 years old.
Betty is of an unknown age.

The call to the ‘fetch’ Method, specific to the ‘Dog’ Class, needs the item being fetched to be specified as a parameter. No additional parameters need to be provided to call the ‘swatchstring’ Method of the ‘Cat’ Class.

dog.fetch('ball')
cat.swatstring()

As with the other Methods, the name Attribute is incorporated in to the output.

Bobby goes after the ball!
Betty shreds the string!