Lesson 10
I am Your Father
Chapter 2: Inheritance
Implementing inheritance
Let us implement some of the classes in our RPG example. We will create a Character
class, and a Enemy
subclass that inherits from Character
. For simplicity, we will only implement a subset of the attributes and methods.
First, we implement the Character
class (with only a subset of attributes/methods).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
We then define the Enemy
class, making it a subclass of Character
(Line 1).
1 2 3 4 5 6 7 8 9 10 11 12 |
|
In Line 3, super()
refers to an instance of the superclass - in this case it is Character
. Think of it as the ‘higher self
’, if that makes sense! You can also use Character.__init__(self, name, health, ...)
, but it is extra work because you have to pass in self
as the first parameter and worry about the name of the superclass. And if the name of the superclass changes one day, you will have to change this line too. So just use super()
to spare yourself all the pain!
Line 3 initialises Enemy
‘s attributes like self.name
, self.health
, self.strength
and self.defence
using the superclass’ __init__()
method. Otherwise you will have to repeat yourself. If you skip this line, then your Enemy
won’t have a name
, health
etc.
In Line 4, you initialise self.evilness
separately, since this is specific to only the Enemy
subclass.
Here is a bit of code to test your two classes. Copy all the code above and below and run it. Observe the output, and understand what is happening!
1 2 3 4 5 6 7 |
|
You might notice that Enemy
has fewer lines of code than Character
, despite being more powerful. We did not have to explicitly define the attributes name
, health
, strength
and defence
in Enemy
. Enemy
inherits all these from Character
, thus a simple call to the constructor of the superclass is sufficient.
evilman
was also able to attack the poor boy
without having to explicitly define the attack()
method.
boy
cannot however express an evil_laugh()
because it neither implemented that method nor inherited that method from any superclass.