Another important concept in programming is Inheritance. This is when you create a parent class and have child classes derived from the parent. The parent class does most of the work while the child classes have their own specialities. Let's clear that up with an example.
We have a superhero class from our previous lessons. Suppose we want a Supervillain class as well. What would we put in this new class? Well, we'd want to convert the name of a villain to proper/title case. And we'd want to specify their superpowers. We may also want the same ranking system.
And that's the problem. Our new Supervillain class would duplicate the code that we have in the Superheroes class. Instead of all this code duplication, we could place shared code in a third class. This third class could be the parent. The child classes, Superheroes and Supervillains, would then have access to whatever code is in the parent class.
To follow along with all this, add a new Python file to your project. Call it Multiverse. In your new file, on the first line, create a new class. Call it Superpowered:
Now cut all the code from the Heroes file but just leave the class Superheroes: part. Paste your Heroes code into the Multiverse file, just below class Superpowered:
Don't worry about the red errors for the Heroes class. We'll fix that now.
Click back on your Heroes file. We'll add a method that will be exclusive to the Superheroes class. First, though, we need to import the Superpowered class from the Multiverse file. Change the first line of your Heroes file from this:
from Multiverse import Superpowered
So we want to ensure that the Superpowered class in the Multiverse file is imported into the Heroes file.
Now we need to change the class definition. Previously, we had this:
However, we want this Superheroes class to be a child of the Superpowered class. To do that, we need a pair of round brackets after class Supheroes. In between the round brackets of your class name, you type the name of a parent class.
Add this as the second line of your Heroes file:
This is enough to create a child class from Superpowered.
To add some functionality to the Superheroes class, add the following code:
def likeability(self, score):
if score is 1:
niceness = "1 YAY out of 5"
niceness = str(score) + " YAYS out of 5"
Your Heroes code should look like this in PyCharm (don't worry about the typos warnings):
Now go back to your MainFile code. It should be this for the hero_one:
hero_one = Superheroes("luke cage",
super_one_name = hero_one.convert_superhero_name()
super_one_ability = hero_one.abilities()
super_one_rating = hero_one.get_star_rating()
The big thing to notice here is that the code still works. We're not getting errors, even though we didn't directly create a class from Multiverse.Superpowered. Our class is created from the child class called Superheroes. This Superheroes class, the child, has all the methods and initialisation from the Superpowered class. The child class has inherited it all from the parent.
Now add the following line to use the likeability method:
super_one_liked = hero_one.likeability(4)
And amend your print line:
print(super_one_name + ': Superpower is - ' + super_one_ability + ', ' + super_one_rating + ", Likeability rating: " + super_one_liked)
The print line is getting a bit long. You can spread the line out over multiple lines. Like this:
Run your program. You should see something like this printed out:
Luke Cage: Superpower is - Impervious to Bullets, Rating: Four stars, Likeability rating: 4 YAYS out of 5
Now let's add a Supervillains class. You could add a new file for this and set up the Supervillains class in there. However, we'll add it to Heroes class, just to show you that you can have more than one class in the same file.
In your Heroes file, at the end, add this line with no indent:
Again, Supervillains is going to be a child of Superpowered. Add the following method to your new class:
def evilness(self, rating):
if rating is 1:
evil_ranking = "1 BOO out of 5"
evil_ranking = str(rating) + " BOOS out of 5"
Your Heroes class should look like this in PyCharm:
You can now put this to use.
In your MainFile, change the first line from this:
from Heroes import Superheroes
from Heroes import Superheroes, Supervillains
Now we're importing both the Superheroes and the Supervillains classes from the Heroes file.
Set up a Supervillain object:
villain_one = Supervillains("mystique", "MG", 4)
Convert the name to proper/title case:
villain_name = villain_one.convert_superhero_name()
The method convert_superhero_name is misnamed. We'd like to rename it to convert_superpower_name. You'll see how to rename (refactor) Python code in the next lesson. For now, add the following line:
villain_evilness = villain_one.evilness(3)
This accesses the evilness method we set up. This method is exclusive to the Supervillains class.
Now print something out:
print(villain_name + ", " + villain_evilness)
Run your code to see something like this:
Mystique, 3 BOOS out of 5
In the next lesson, you'll learn how to refactor your code.