video

Lesson video

In progress...

Loading...

Hey, ya.

Welcome back to Object-Oriented Programming.

I'm Mac, your computing teacher for this unit.

In today's lesson, we're going to look at one of the key principles of Object-Oriented Programming called inheritance.

And we'll build off of the experience you got last lesson making your own class.

For today's lesson, you're going to need a notepad and some pencils.

So you can make notes and answer questions while you're learning.

And I'd also like you to remove all distractions from around you, this includes your mobile phone.

For the tasks you're going to complete today, you're going to need a repl.

it account, so that's R-E-P-L.

I-T.

And to get an account, you're going to need to ask your parent or carer.

So make sure that you've done that before you start today's lesson.

As per usual, I've got my water here and I want you to make sure that you've got some refreshment, if you need it throughout the lesson.

If you'd like to pause the video here, go get everything you need, come on back when you're done and we'll get started.

In today's lesson, you're going to define the principle of inheritance.

You'll also define what's a superclass and a subclass, you'll then select appropriate uses of inheritance inside of a programme that uses Object-Oriented Programming.

And we're going to create a subclass, all of our own, inside of a programme.

First activity is identifying object-oriented code.

Now I've done a lot of practise looking at different object-oriented pieces of code and also creating our own.

So I'd just like to recap that a little bit before we start today's lesson.

If you want to pause the video here and head over to your worksheet, I've given you a few tasks over there, to trace this programme written with object-oriented programming and identify some key parts, some key terms that you've learned throughout this unit.

So yeah, pause the video here.

Go do your task and then come back when you're done, we'll go through the answers.

Welcome back.

How'd you get on? Let's have a look.

So the first thing I asked you to identify was a class.

So given this programme here, can you spot a class or give me the name of a class? So let's have a look.

Vehicle is the class in this programme, you can tell because it has a capital V.

So has a capital letter, which is one of the conventions we follow when writing a class, it's also being imported on this top line here.

So that gives you an indication that is coming from another file, which is what we've done with all of our classes, so far.

One last clue might have been the constructor on line three, used to create a new object.

It also uses that name Vehicle.

So that could've given you a clue that this was a class.

Let's have a look at the next one.

Next, I asked you to identify an object in this code.

So an object being an instance of a class, one that we can manipulate inside of our programme.

So, did you spot that one? Car was the object in this code.

We can tell because it's a variable that is being populated with our constructor, that we looked at in the last one.

So that Vehicle with all of these attributes, "BMW", 4, 120 is got to be stored in that variable called car.

So this is the object.

You can also tell 'cause it's used a bit later to access some attributes and methods.

Speaking of which, I asked you to identify some attributes in this code, so, how'd you get on? Let's have a look.

There are two attributes in this print statement here.

So you can see that we're using the getters, which is part of how we create classes.

We make getters so that we can access the attributes and there's one for getName and getSpeed.

So we know that name and speed are probably two attributes on this Vehicles class.

Finally, I asked you to identify a method in this programme, and hopefully you guys spotted that right at the bottom here, we have the drive method on the object car.

So there's lots of clues that could have led you to find these key pieces of a programme.

Hopefully you were able to identify more.

If not, just double check you're working now and highlight them, and leave yourself a note so you'll remember later.

Now, let's keep going.

The next activity is looking at this principle of inheritance.

What is inheritance and how can we use it? If you cast your mind back, I know it feels like ages ago, it does to me anyway, all the way back to lesson one.

And in lesson one, no wait, lesson two.

In lesson two, we looked at the attributes and behaviours of animals.

And we spoke about how we can turn them into a class.

When I actually came to making the class, we created a Pet class.

And that Pet class kept information about our pets, such as their name, what kind of animal they were, things like that.

Now we could have created an Animal class and then used inheritance to create a Pet class that is derived from it.

So we could have made that top level Animal class and then used inheritance to copy that class, and inherit all of their attributes and methods into the Pet class.

Now, this principle is called inheritance.

We can base one class off of another and the new class will inherit all of the attributes and methods that are contained in the original class.

And you can add new methods or new attributes as you need for that specific case.

The original class is called a superclass.

So the one that we're basing the new one off off is called a superclass.

And the new class that's created using inheritance is called a subclass.

Now, I have a question for you here and you can pause to think about it, if you like, which is the superclass in our example? So I spoke about Animal and Pet, which one of those would be the superclass if we had gone down that method? You can pause it and think about it a little bit.

I want you to point to the one on screen you think is the superclass.

Are we ready, are we pointing, you got it? Three, two, one.

Cool.

In our example, Animal is the superclass.

It's what we're basing our new class Pet off of, so it's called a superclass.

And you can see in this diagram on the right-hand side here, the hierarchy.

Now you'll notice the arrows point up and this is always done in class diagrams to show which class is a part of the superclass.

So you can see that Pet is inheriting from Animal because we can follow the arrow.

Now, the reason that this structure exists is because pets are a subsets of animals.

They will have a lot of the same attributes and methods that animals will, but they might have specific ones that only they can use.

There are more specific to animals that are pets, and not something else.

And we could make other subclasses, we could make that animal class and then we could derive some more classes off of it.

Again, pause it here and we'll have a little think, what other sort of subclasses could you make? So if we made a Pet subclass, what other sort of categorizations could you have for Animals, think about it a little bit and then resume the video.

Hopefully you were able to come up with a few, let's have a look.

So we could've made a Wild class, so that could have counteracted with our Pets.

We could have some animals that are Pets and others that are Wild.

You also could have gone more traditional classification, so we could have said Mammals and Reptiles and Amphibians and Insects, and those could all be kind of subclasses coming off of that Animal class.

The idea here is you can see how we could easily put a kind of hierarchy together, where we have an Animal and then subclasses that derived from that class.

So I've added Wild to the diagram here.

So you can see that there are now two subclasses that are based off of this Animal superclass.

As said before, when you base a class off of another, it inherits all of the attributes and methods of the superclass.

So if the Animal had an attribute, those species, both Pet and Wild would also have that attribute and we wouldn't need to define them again.

When we use inheritance, the programme knows that it needs to copy over all of the attributes and methods from the original class to the new one.

So we don't need to define them again.

So hopefully you can see how this might save you some time.

And we can add new attributes and methods as well to the subclass that the superclass does not have.

We can add extra things like, specific data points or actions are only applicable to the subclass.

So they only work for Pet animals or they only work for Wild animals, but don't have to be shared by all animals in general.

So we can kind of get a bit more granular with where our attributes and methods go.

So I can also even make new subclasses by using Pet as a superclass.

So at the moment, Pet is a subclass of Animal but I could make two new classes, Cat and Dog, which are subclass of Pet.

That would mean Pet becomes a superclass to Cat and Dog, and Cat and Dog would be the subclasses, in this example.

Now Animal has no relation to Cat or Dog.

All we know is that Cat and Dog is a subclass of Pet and that Pet is a subclass of Animal.

All right.

And the most amazing thing here is that the new classes would have all the properties of both Animal and Pet.

So those Animal would pass its attributes and methods onto Pet.

And then when we made the new subclasses, Cat and Dog, they would also inherit those attributes and methods.

So we made a species, class and Animal here, Pet would have it, but so would Cat and Dog.

Hopefully that makes sense.

Now, I'd like you to pause the video here, we've done a lot of thinking, you've had to listen to me talk quite a lot.

So I'd like you to pause the video here, head over to your worksheet.

I've got a task on there for you.

I'd like you to identify which class an attribute and a method should belong to.

You got to think, does the action or attribute belong to all or just one of the classes? If it belongs to multiple classes, where should it go in the hierarchy? Pause the video here, go have a go, come back when you're done and we'll keep going.

Welcome back.

How'd you get on? Let's have a look where the data and behaviour should go.

So I asked you for an attribute called number of legs, which class should it belong to? Should it be in Animal, Cat, Dog, or maybe Pet? Now, hopefully you noticed that number of legs is something that all Animals share.

All Animals have a number of legs, even snakes, which that number would be zero.

But this is a piece of data that would apply to every class in OUR hierarchy.

So we would give it to Animal, and then all the other classes would inherit it from there.

The next one is, I asked you about a method called wag tail and where you think this would go in our hierarchy chart.

Now, hopefully again, you noticed that wag tail only really applies to Dogs.

Cats don't really walk theIR tails, not in the same way anyway, certainly not all Pets do.

Lizards and fish and stuff definitely would not wag their tails, and all Animals don't necessarily wag their tails either.

So really this one should sit inside of Dog.

So we would define it as a new method that only Dogs could use.

One final question on this whistle-stop tour of inheritance.

Hopefully you understood the idea that we can base one class off of another, and we don't need to write the attributes and methods for that original class again, they're just inherited.

So what benefits can you see to using inheritance in your programmes? Remember, thinking back to those conventions that we spoke about a while ago, I want you to think about both the writer and the reader of a programme.

What benefits are there? Well, if we're basing our new class off of another we avoid repeating code.

So if I want a method that my Animal shares with all these other classes, I only need to write it once inside of Animal, and then all the other classes will inherit it.

It also allows us to organise our classes and objects into a hierarchy, like the one we've got on the left here.

It allows us to think about how our programme will interact and lay it out based on a hierarchy.

And this works really well for some programmes.

We can also easily share methods and attributes between different classes.

So if I wanted to make sure that there's a specific method on both Cats and Dogs, that they might not share inherently if I wrote them on their own, I can use inheritance to make sure that that method is there so that I can use it on both Cats and Dogs.

We've heard a lot about inheritance now, let's have a go at improving our Monster Quest game and using inheritance to do it.

So the game designers loved what you did with your first round of Monster class on Monster Quest.

And now they want you to implement inheritance into the game.

And then we need to create two new subclasses of Monster.

Friends and Enemies.

Now, if you remember Monster Quest is going to be a puzzle game where players need to interact with different Monsters.

And now they wanted to figure out whether they are a Friend or an Enemy.

And the way they'll do this is the player will talk to the Monster first, and the player will then decide whether to high high-five or to fight the Monster.

If the player high-fives a Friend Monster they'll become Friends, the Monster will give them some sort of gift.

And if the player fights an Enemy and wins, they will scare the Monster off.

So adding a bit of interaction into our game.

But both Friends and Enemies need to be Monsters, they all need to have that kind of central, those attributes and methods that we defined in the last lesson.

But they're going to be slightly different, so we're going to use inheritance to do this.

Well, the first one, I'm going to show you how to do it and you're going to follow along, and you're going to see a live coding demonstration.

Then the second one, I'm going to leave up to you.

So let's recap what I need my Friends subclass to do.

So I need it to say something when the player high-fives them.

So I need there to be some sort of interaction there.

And then I also need them to become Friends with the player and give a gift.

So, let's have a little think about what we're going to implement.

Now, you're going to watch me code it but I'd like you to have a little think first about what attributes and methods we need.

So pause the video here, head over to your worksheet.

I want you to think about what attributes a Friend would need to meet those requirements.

Come on back when you're done and we'll go through it.

Welcome back.

How'd you get on? Let's have a look.

So if the Friend subclass needs to say something when the player high-fives them and they need to give them a gift, I probably need two attributes to do that.

I need some high-five dialogue, something for the Monster to say when the player high-fives them.

And I also need a gift, some sort of string, I'm going to write out the name of an object you know, like a cake or a hula hoop or something.

That the Monster will give to my player to show that they're Friends now.

It will still need all of these original attributes and methods.

So it's still need its name, its health and a line of dialogue they will say when the player first meets them.

But we're going to add some extra attributes using inheritance.

So I'd like you to pause the video again here.

And now, I want you to head back to your worksheet and think about what methods my Friend needs.

Resume when you're done and we'll go through it.

Welcome back.

Let's have a look.

So I only need one extra method.

I just need a high five method.

And that's what's going to get called if the player decides to high-five my Monster.

So it's going to do something like, say the line of high-five dialogue and then say the Monster's name and gives you the gift.

So we're going to let the player know that they've high-fived the Monster and that they're Friends now because they've got a gift.

I'm going to head over to my rappel and we're going to get coding.

Make sure you've got your project open as well, so you can follow along while I do.

Now, we're back in my rappel.

So hopefully you should recognise this from the end of the last lesson, when we created our Monster class.

So in here, this is my main.

py file.

I've got all my Monsters that we created.

And if I head over here to my Monster file, you'll see that we've got class including all the methods that we wrote last time.

So I'm going to add the Friend subclass now.

I'm going to pause at certain points as I'm going through to allow you to write this out in your project.

So if you want to pause the video here and just make sure you've got this file open as well, just so you're ready to go when I pause the video, that's fine, pause it now.

Get to this point, press play and we'll keep going.

Hopefully you all have this open.

Now don't try and do it at the same time as me, remember, I'm going to give you pause points.

So just concentrate on what I'm doing and just think a little bit about the steps that I'm taking, what I'm doing and any of the points that I highlight to you.

So the very first thing is, you'll see I'm inside my Monster class.

I'm going to go right to the very bottom of the file.

So underneath where I've declared my Monster class, I'm going to declare my Friend class.

And we do this in much the same way, so I'm going to write class and I'm going to get a name, I'm going to call it Friend.

Now, the main difference when we're creating a subclass is that inside these brackets here, so you'll see that in Monster these brackets were just empty.

But if we want to create a subclass, we can put the name of the superclass in between here.

So if I write Monster, this Friend or subclass will now inherit all of the attributes and methods from Monster.

So that is how you declare a subclass.

So you give the subclass a name and then inside the brackets you put wherever you want to base the class off.

In this case, we want Friend to be a subclass Monster.

Now, we just need to write the new constructor for our Friend subclass.

And we do this much in the same way that we write our constructor for a regular class with a few exceptions.

So first we're going to define it using the same keywords.

So __I-N-I-T or Init.

So it's just the same.

You'll see that if we go back up here, it looks just the same so far as our original one.

And then I'm going to give the self parameter, still, every single method we write needs that self parameter, remember that.

When we create a new subclass, we actually still need all of the attributes from the original class.

Because if it's inheriting them, we still need to assign them a value.

So I'm going to add those in first.

So we had name and we had health and then we had speech, were our original ones.

So again, if I scroll up here, which you should do too, use this as a basis.

We can see that they're the same here.

It's got name, health and speech, name, health and speech.

But then we also wanted to add our two new attributes.

So we had one that was high-five dialogue and, final one, tell you what, let me expand that out a little bit so we can see it all on one line.

There we go.

So high five dialogue and then we also had gift.

So the high-five dialogue is what they're going to say when the player high-fives them, and then gift is what we're going to give them to show that they're best friends now.

I have declared my constructor.

The very, very first thing I need to do is I need to initialise the superclass.

So the moment I've created a constructor, we're going to create a new Friend subclass but it doesn't really know what Monster is yet.

So I need to activate it by using the super keyword.

So super just means whatever is my superclass initialise it.

And we can say.

__init__ So I'm calling this method on the top here, and then I need to hand it my parameters.

So I've said I'm going to get a name, health, and speech.

So I need to make sure those go through to my superclass.

And you'll notice I'm not using self here, and that's because Python will know to send the self as a parameter regardless of what I do.

So I'm just going to put in the three attributes that it needs for the superclass.

Now I still have high-five dialogue and gift to sort out.

So I'm going to assign those in the same way we would for a normal class.

I'm going to say high five dialogue equals high five dialogue and then self.

gift, no, minus the gift.

So I've initialised my superclass and I've given it my three attributes that it needs for up here.

So this method is going to be called and then I've also assigned my two other attributes.

If you'd like to pause the video here and just write this bit out in your programme, as just as it's written here.

Make sure you've got that Monster in the brackets and you've initialised the superclass.

Resume when you're done and we'll go on to doing the getters and setters.

Hopefully your file is looking the same as mine.

Let's move on to the next bit.

So now I need to create getters and setters.

Now I don't need to create getters and setters for my old attributes, for the ones for Monster, the name, health and speech.

Because my new class is going to inherit all of these methods, including the getters and setters.

So I don't need to getName, getHealth, getSpeech or setName, setHealth, setSpeech, instead, I just need to make getters and setters for these two new attributes.

And that's my getters and setters.

So, again, if you'd like to pause the video here, write these into your programme.

You can resume when you're done and we'll keep going.

Awesome, well done guys, you're doing really well so far.

Let's just do the last little bit of this new class.

I now need to do my new method and you remember the high five method needs to do two things.

It needs to say the line of high-five dialogue and then it needs to give the player my gift.

So first, let's create a high five method.

I'm going to pass it through self.

It doesn't need any external parameters at all.

It just needs the self parameter so that I can use those attributes.

And then I'm going to say print and I'm going to use it's name.

So I'm going to say, I thought, let's say, print something says and that's something is going to be the Monsters names.

So this will say something like, AJ says and then the next line I will print their high-five dialogue.

I hope it's all correctly.

Next I'm going to print that dialogue.

And then finally I'll say, they give you a, and then another %S", cause I'm going to replace this and this time I'm going to replace it with self.

gift.

Which, make sure I've got my percentage sign in the middle there.

So my high-five dialogue will say, the Monster's name says, it'll print that line of dialogue and then it'll say, they'd give you a, and then we will give them a gift.

And that's it.

That is my Friend subclass made.

So you can see I've initialised the superclass here.

I've set my new attributes.

I've got getters and setters but only for the new attributes.

And then I've just done my new method.

And you also notice that I've used self.

name in here because I know that my new class will have a name attribute, because it's inheriting it from the Monster.

Awesome.

You want to pause the video again here, update your file so it looks just like mine.

And then we're going to test this new class out before you write the Enemy class on your own.

Go for it.

Welcome back, hopefully you got that Friend class in there all good.

I'm going to head over to my main.

py file.

I'm actually going to delete everything from the bottom here because I just want a completely fresh new file.

The very first thing I need to do is I need to import my new class by saying Friend at the top here.

So from the file called Monster, import the class Monster and the class Friend, then I'm going to create a new Friend.

So let's make a vampire, then I'll use my Friend class.

So this is a brand new constructor here, and I will give it a name.

So the, is going to be called Vlad.

That's a very vampirey name.

Then I need its health points.

So he's going to have a 100 health points, line and speech will be, I vant to suck your blood.

But because he's a Friend, I also need to give him some high five dialogue.

And what he's going to say is maybe, I don't want to suck your blood.

But because he's a Friend and then he's going to give you a gift.

And the gift for this vampire is going to be a toy bat.

I don't think vampires quite like toy bats.

And now let's test this out.

First of all, let's say, let's print the vampires name.

A vampire.

getName.

Remember, we never defined getName inside of our Friend class, have a look back down here.

Look, there's no getName in here.

Now you've seen me create the Friend subclass and following along for yourself, I'd like you to make the next subclass on your own and that is the Enemy subclass.

So the Enemy is going to work a little bit differently.

If you remember, each Enemy is going to have a weakness, 'cause you're going to be able to fight the enemies.

So each Enemy will have a different weakness.

You're going to ask the player to type in a weapon of choice and they will defeat the Enemy if they choose their weakness.

So every Enemy has a particular thing that will let you win the fight with them.

And you're going to store that inside of Enemy as a weakness.

And then inside your fight method, you are going to compare whatever the user types in with the Enemy's weakness.

If they're the same, the player wins, if they're different, the Enemy wins and attacks the player.

So you can see in this example here, Dave is obviously has a weakness for cheese and Brian is weak to cats.

If you'd like to pause the video here and head over to your worksheet, I've given you step-by-step instructions and what you need to do to make the Enemy class.

Follow along with those, use your Friend's subclass as an example and work off of that.

Resume the video when you're done and I'll show you my Enemy class and you can compare to yours.

Welcome back.

How did you get on? I hope you enjoyed making the Enemy subclass.

Let's have a look at mine.

So it's right at the bottom of my Monster file.

I've got my Enemy subclass.

And again, it's inheriting from Monster, it has two new attributes, fight_dialogue and weakness.

So it needs to say something when the player fights them and then they have a weakness, the thing to compare to.

So again, I'm going to initialise the superclass just the same way I did for Friend, look up here.

And then I'm going to set the two new attributes.

I created getters and setters for each of the new attributes as well.

So getFightDialogues, setFightDialogue.

And then the Piece de resistance, my fight method down here.

So when we fight a Monster, they are going to say their name, first of all, and then their line of dialogue.

The thing that they say when they fight them, we're then going to ask the player what weapon they choose.

The player then inputs a weapon and we compare it.

So I've used the.

lower method here to make sure that both the weakness and the thing the player types in are all lower case.

Just means that if there's a capital letter, it'll still work.

I've then printed a helpful message for the player and then said, you are victorious.

If that's not the case and they've lost the fight, I've said, you wave whatever the weapon is at self.

name.

You don't have to have done this the same as me.

You might have done a slight different print statement, that's fine.

This is just one way to do it.

And then I get the Monster to laugh at them and then it just prints You Lose.

So if you'd like to pause the video here, I would just make sure that's all in view for you.

So if you want to pause the video here, type this out into your own thing, resume when you're done and we'll just do one last level test on this Enemy.

So that should work.

Oh, another thing I need to make sure I do is import my Enemy at the top here, so that the rest of my code knows what to do and then let's do a quick fight test.

Let's see what happens.

Run my code.

I should see, Vlad has said hello to me, what weapon do I choose? I'm going to choose garlic.

And I wave my garlic at Dave, Dave laughs at you and bites your head off, I lose.

Again, If you want to test your Enemy class out, do that now.

Pause the video, head back when you're done and we'll wrap this lesson up.

That's all from me today.

Thank you so much for sticking with me.

I hope you enjoyed using inheritance to improve Monster Quest.

Make sure you take the exit quiz before you leave.

And the very last thing I'd like to ask you is, if you'd like to, please ask your parent or carer to share your work on Instagram, Facebook, or Twitter, tagging @OakNational and #LearnwithOak.

I'd love to see your Monsters, your Friends and your Enemies.

One more lesson this ROP unit and I'll see you there.

Until then, happy learning.