Back

Inheritance

Introduction
At the moment, we only have one class for monsters. In our game, all monsters will have a name along with strength, wisdom and magic attributes. Suppose we want to create other types of monsters, for example, dragons and orcs. They will have all of the attributes of monsters, plus some extra ones. A dragon will have an attribute that tells us whether it is an adult and another one for speed. Orcs have different weapons so we will store whether or not they have a sword, dagger or sling. Because dragons and orcs have all of the properties (the attributes and the methods) of monsters, we don't need to write a class that includes these. We can write a class that just inherits everything from the monsters class, and then just add the extra attributes and methods that are needed for that particular class. We can represent this idea as a diagram.

inherit1

The diagram is called unsurprisingly an inheritance diagram. It shows you that the main superclass is 'monster'. It shows that the subclasses are dragon and orc and these inherit all of the attributes and methods from the monster class. In each box, you can see what attributes that class has and its data type. We don't show any inherited ones, though. We also show what methods a class has and what attributes it is expecting for it to work, although we haven't included the constructor and __str__ methods here, to keep the diagram nice and simple.

Inheritance
Let's add another class for dragon. We will put it under the class for monsters.

inherit2

Line 44 above is the class header. When we created the monster class, there was nothing in the brackets. This time, because we want to inherit all of the attributes and methods from the monster class, we simply need to put monster inside the brackets. Line 45 is the constructor method for the dragon class. This immediately calls the constructor method for the monster class. We have then added the extra attributes that we need for a dragon. Notice that we have also added a __str__ method to print out all of a dragon's details.

So what attributes and methods does the dragon class have now? It has all of the ones from the monster class and the extra ones that we added. We can prove this, of course by using help. At the end of your code, type in:

help (dragon)

and run the code. You'll see that the dragon class has inherited the i_strength and d_strength methods, for example.

Instantiating a monster
Try adding some dragons e.g.

inherit3

Don't forget to add some instructions to print out the dragon objects! You can add some dragons with pre-fixed values, or add some with random values and see what happens.

Try these tasks.
1) Add an extra class for orcs and make up another monster of your choosing.
2) Create some orcs and other monsters and print out their details.
3) Change the code to set-up some fights between different monsters.
4) Add some functions so different monsters can fight using an attribute other than strength e.g. magic.

Back