Skip to 0 minutes and 1 secondWe'll make a subclass of character called enemy, which will use the character class as a basis, but add some more functionality specific to enemies. In the existing character.py file, start a new class below the character class. The name of this new class is enemy. But we've put character inside the brackets to tell Python that the enemy class will inherit all of the attributes and methods from character. So character is called the superclass of enemy, and enemy is a subclass of character. Now, let's write some code to create a constructor for an enemy object. This looks identical to the constructor for character. Our enemy class inherits from character, so character is the superclass of enemy.

Skip to 0 minutes and 54 secondsInside the constructor, we call the superclass constructor method. This line of code means to make an enemy, first, make a character object, and then we'll customise it.

Skip to 1 minute and 7 secondsSave this code. Now, let's prove through using these two lines of code that the enemy class has inherited all of the properties of the character class. Go back to your test file and change all references to the character class to instead be references to the enemy class.

Skip to 1 minute and 27 secondsRun the code and test it out. You should be able to see the description of Dave and talk to him just as you did before.

Inheritance – extending the Character class

catrina

In our game we want to have multiple characters to interact with. For example, I might create two characters: Dave, the smelly zombie, and Catrina, the friendly skeleton. I might want to fight with enemies such as Dave, but I wouldn’t want to fight with friendly characters such as Catrina.

We will make a subclass of Character called Enemy. It will use the Character class as a basis, but add some more functionality specific to enemies. In the existing character.py file, start a new class below the Character class.

3.4 add code screenshot

class Enemy(Character):

The name of this new class is Enemy, but we have put Character inside the brackets to tell Python that the Enemy class will inherit all of the attributes and methods from Character. Character is called the superclass of Enemy, and Enemy is a subclass of Character.

Now let’s write some code to create a constructor for an enemy object – this looks identical to the constructor of Character:

 def __init__(self, char_name, char_description):

As we said, Character is the superclass of Enemy, so we need to ensure that Enemy inherits from Character. To do this, we have to call the superclass constructor method inside the constructor of the Enemy class.

super().__init__(char_name, char_description)

This line of code means “To make an Enemy, first make a Character object and then we’ll customise it”.

Save this code. Now let’s prove that, with the help of these two lines of code, the Enemy class has inherited all of the properties of the Character class. Go back to your character_test.py file (or main.py in the new trinket) and change it so that all references to the Character class become references to the Enemy class:

from character import Enemy

dave = Enemy("Dave", "A smelly zombie")

Run the code and test it out. You should be able to see the description of Dave and talk to him just as you did before.

An object of a subclass is also considered to be an object of its superclass. This concept is called polymorphism. In simpler terms, a <subclass> is a <superclass>.

So in our example, an enemy is a character. If a function requires a character object as a parameter, we can give the function an enemy object instead and the code will still work. However, this does not work the other way around – if the function requires an Enemy object, we cannot give it a character object, because Enemy is a more specialised version of Character.

Share this video:

This video is from the free online course:

Object-oriented Programming in Python: Create Your Own Adventure Game

Raspberry Pi Foundation