video

Lesson video

In progress...

Loading...

Hey ya! Welcome back to Lesson 2 of "Object-Oriented Programming." I'm Mac, your computing teacher for this unit.

In today's lesson, we're going to be looking in depth at causes and objects and you're even going to programme a class and create objects of your own.

For today's lesson, you're going to need a notepad and a pen, so you can take notes and answer questions while you're learning.

You're also going to need a "repl" account.

So that's R-E-P-L-.

-I-T.

And in order to get an account, you need to ask your parent, or carer.

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

I'd also like you to remove as many distractions from around you as possible.

And this includes turning off your mobile phone.

As always, I have 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 and go get everything you need, come on back when you're done and we'll get started.

In this lesson, you're going to define the terms, class and object which are two fundamental building blocks to object-oriented programming.

We're then going to model a real world problem, using object-oriented programming techniques.

You'll create a class all of your own, and then you'll use a constructor, a special method, to make objects.

It's going to be great, so let's get right into it.

The first question I'd like you to consider is "What is an animal?" We're going to create an animal class using object-oriented programming, but before we can do that, I would like you to pause the video here, and make some notes about how you would describe an animal to somebody who had never seen one before.

Be as general as you can but try and think of terms of data and behaviours.

So what does an animal have and what does it do? Right, what is an animal? So, pause the video here, make some notes and then come back, and we'll have a discussion.

Welcome back.

Sometimes it's a bit weird to think like what somebody had never seen an animal but I hope you were able to get some notes down.

Let's have a look.

So you might have said that an animal is a living creature, that it can be covered in fur, feathers, scales, or maybe even an exoskeleton.

They vary in size.

Some have legs and some have more than others.

like a millipede, for example, some can fly, It's like birds and insects.

They breed to produce offspring, so they reproduce in some way, and then they eat food to survive.

You might've had some other ones, these are just some examples, but the idea is, there are some common traits that animals share.

The next thing I'd like you to consider, is how we can generalise these statements a little bit.

So we're going to pause the video again here and have another go at a task.

I'd like you to have a look at the list that you've just made in your notes, and I want you to circle or underline the things that all animals share in common.

But I want you to specifically focus on properties and actions that all animals share.

So what's the animals have and what can they do, that's the same across all the animal species? Pause the video here, have a go at that, and come back when you're done.

Right, welcome back, how did you do? Let's have a look.

So all animals have a species type of some description, they eat food or eat something anyway, they move, and they can reproduce.

So you can see, we've just taken a bit of a wild ideas about what an animal is, and we've paired them down into some actions and properties that all animals share.

And these are the building blocks that we need to turn this into a class in object-oriented programming.

Which is the topic of our next activity, we're going to have a look at object-oriented programming, we're going to reiterate some of the terms that you learned in the last lesson.

And then we're going to start you off in making your own class.

So some key words to start us off with.

In object-oriented programming objects are used to model things in code.

When we say we model something, it just means that we replicate some real world system or object, inside of a computer, just like our animals.

An object is a collection of data.

Each object has the same basic attributes but the values might be different.

It also has associated actions called methods.

Let's have a look at an example.

So here we might have car objects and their attributes might be something like the make, which company made it, you know, model, you know, which version is it of those companies cars.

The colour, you might also have how many wheels it has, you might have how many doors, right? Is it five, three or just two? There's also some associated actions that all cars share.

They can accelerate, they can slow down and they can change gear.

So if we were to create a car class, we would define the make, the model and the colour, and then we would make individual objects.

So you see the top point here would have its colour set to green, the middle would be black and the bottom would be beige.

They're all formed from the same template.

Another example, we could make houses, so each object is a house and they all have an address, they have a height, they have a width, they have a colour of the outside and maybe they have a type too, right? Is it a bungalow? Is it a flat? Is it a two-story? Does it have a garage? Right, all this stuff will maybe come under type.

Then they have some methods, some actions, that we can do with each house.

So we can open and close the door, or we can open and close a window, for example.

So hopefully these illustrate to you how we can model real world things with object-oriented programming.

Now these are just objects that individual things that have a set of attributes and have some actions, but how do we define those attributes and those actions? Well, we do that using a class, which you can think of as a blueprint for creating as many of the same object as you like.

It's a little bit like a cookie cutter.

Each new cookie made with this template is a new instance of the same class, called an object.

Now I'm going to show you a short video, that hopefully will illustrate this concept a little more for you.

I hope you enjoyed that, as we continue through this unit, continue to think about that analogy of a cookie cutter and cookies.

So a class, is like the cookie cutter we define the shape is and then we can use that to create as many objects as we want.

It'll really help you understand how object-oriented programming works.

So, how do we create a class? And then how do we make objects out of it? Well, inside of a class, we need to define a constructor.

Now, a constructor is a special type of method, which I'll remind you, is the actions associated with the class.

It's a special kind of method that initialises, which is just a fancy word for constructs, an object of that type and defines what attributes the object has.

When we call this method, a new instance of the class is created, and we call that instance an object.

So for example, we might have a cat class and then we create a new object that holds my dear Fishcake here.

Our Fishcake has a name he's called Fishcake.

He has a colour, 'cause he's ginger.

And he also has a description, right? We can write a little bit about him and we can store that as a piece of information about Fishcake.

Now we're not actually a cat inside of the computer, we're just capturing some specific pieces of data about that cat, that we want to use for something.

So we're going to have a go at this now.

What I'd like you to do is open up repl.

it and create a new Python repl, I'm going to load up mine and I'm going to see you back here in a second.

Right, I've created myself a new blank Python repl.

You should be looking at a very similar screen to this, a completely blank canvas with only one file on the side called main.

py and the very first thing to do is we need to create a new file.

That's 'cause we want to define our class, away from the main programme, and then import it, so we can create objects.

So stage one, is to create a new file and call it pets.

py Once you have that, you're ready to define your class.

And we do this using a new keyword, and it's "class," we're just pretty self intuitive.

We're not going to give a call a name, and I would like you to call this class, "Pet," with a capital "P" remember, because that's a convention, we always give our classes a capital letter.

And then the last little bit of syntax is two brackets, and then a colon.

Just like all other structures in Python, we put colons so that the interpreter knows that there's going to be a block of code underneath it.

Next we need to create our constructor, and we do that just like we define any other subroutine in Python, using the "def" keyword.

Now there's a specific way to refer to a constructor.

And that is with two underscores, make sure it's two, this is really important.

Two underscores, and then "init" and then another two underscores.

These are special methods in Python, that you don't call by name.

I'll show you that bit in a minute, and let's just get this run out first and I'll show you how we use the constructor in a moment.

Next we create brackets, just like we do for any normal procedure or subroutine, and then we need to give it some parameters.

Now we have some specific attributes in mind for our pet, but the first thing we need to write is "self." we'll go into this a little bit more in future lessons, but every single method inside of a class, in Python, it needs this "self" parameter.

And this is just a way of each object knowing how to call itself.

So if I want to access an attribute on that specific object, it has a reference to itself.

You can just think of "self" as meaning, this object.

So we want "self," we also want a name, we want a species, and a description.

And we just put those in with commas in between of them, and then add a code in to the end.

We're going to put some more code inside of it.

I'm just going to widen my block here a little bit, so we can see the whole line at once.

There we go! So, now we need to assign those attributes on to my object.

And the way I do that is by saying "self." So this object.

name, so create a name variable on this object and inside of it, store whatever I get inside of this name, parameter up here.

Now this can be a little bit confusing because obviously the name up here and the name here, are the same.

But until we do this line, the name variable has not been created.

And then all we're going to do, is we're going to go from here and we're going to add the two other attributes as well.

So I'm going to say, self.

species = species So on this object, create the variable called "species" and set whatever we get inside of this parameter "species" as the value, right? And then the same thing description, okay? So whenever we're referring to variables inside of an object, we're always going to use "self." whatever the name of the attribute is.

Right, pause the video here, head over to your worksheet 'cause all of these instructions are on there for you too.

And I want you to get your file looking the same as mine.

Remember really, really important, remember that there's two underscores either side of "__init__." And that just stands initialise, right? Which is a fancy word for construct.

Cool, pause the video now, get your file looking like mine, resume when you're done, and we'll keep going Right, hopefully you got your file looking like mine.

Now that we've defined a new class, and we've made a constructor, we can test it out and make a pet.

So the first thing to do is to head over to your main.

py file.

Remember, we're not going to do this inside of "Pets," We're going to use our class inside of "main." Before we can use the class, we need to import it.

So, I'm going to say, from pets, now notice, "pets" is the same as the name of my file without the ".

py" file extension.

This is how I access things in a separate file.

I just give the name without the ".

py" file extension.

So from the file, pets, import.

Oh! from pets import Pet now notice my pet has a upper case P and that's how I know it's the class.

So from the file, pets import my Pet class.

And now I'm going to make a Pet.

Now, I'm going to call this Pet.

Now, I'm going to call this Pet, "my_cat." And "my_cat" remember the name of the variable that we must call the object in.

I'm going to use the name of the class to activate the constructor.

Now you'll remember I said, inside this constructor here, this double underscore, means that that Python will treat this method specially.

And what it does is, it masks this method with the name of the class.

So when I say "Pet" in this file, I'm actually calling the underscore underscore init method.

So, now I need to give "my_cat" some attributes.

Now, I don't need to pass the "self" parameter to this method, because Python will do that for me.

It knows to look for it and it'll pass a reference to the object.

So I just need to give it a name, a species, and a description of some kind.

I'm going to call my_cat "Fluffy" I'm going to say it's a "Cat" as its species, right, it's a cat, and I'm also going to write a short description.

So, it's going to be, "Black and white with long hair" So, we're now create an object, it's going to have the attributes, Fluffy is its name, cat it's species, and his description is, "Black and white with long hair." Now, if I run this code, you'll see that nothing really happens.

And that's because we haven't made a way to access the attributes yet.

So, that's what we're going to do next.

I'm just going to head back over to the slides to explain how to do that.

Right, now we've got a constructor in place, we need to add a couple more bits before we can test out our new class.

The first thing, is we need to create some getters for the object.

In order for us to make use of the information stored on object, inside of our main programmes.

This is a way from where we just defined our class, we need to write some methods that give us access to the attributes.

So it's really bad practise to access attributes directly by inside of your main programme, accessing ".

name" variable, instead we create new methods called get_name, get_species and get_color that when called will return the value stored in the attribute to us.

We call these getters, because obviously we're getting information out of it.

Right, I'm going to head back over to my Python repl now, and we'll do this.

Right, so how do we add getters to our new class? Well, first of all, we come out the constructor and make sure they all the way lined up with that first "def." And then we're going to create some more methods.

So again, we'll use the "def' keyword.

Now, hopefully you should be familiar, with creating functions and procedures inside of Python and through your programming classes.

If you're not, go over and check out some of the programming modules here on Oak National Academy, and then come back to this lesson afterwards, because I'm going to assume that you know how to write methods.

So, we're going to create some new methods, and I said that they should be called "get." And then whatever the name of the attribute, that you're accessing is.

So, let's just do get_name to start.

And then we're going to open up bracket, just like we do with every other procedure and function, And we're going to make sure that we use that "self" parameter, well not "slef," the "self" parameter, again, this is how we access attributes, we need to have that "self" attribute, and "self" parameter in there.

So when this method is called, I want to return whatever is inside, my name attribute.

So, I'm just going to use the "return" keyword, and then "self.

name" so it's going to return whatever's on this object stored in the name variable.

Now you need to do the same thing for both species and description.

I'd like you to do that independently.

So pause the video here, and make sure first of all you have your get_name method in there, and I'd also like you to do one for species and another for description.

resume the video when you're done and we'll carry on.

Right, here you can see an example of the three getter methods written out.

Hopefully you've got something similar.

Now the pattern is the same each time.

I call the method, get, and then the name of the attribute, this makes it easy for somebody who's using my object to be able to know which method maps to which attribute.

Each of them needs this "self" parameter.

And we also need to make sure that we return the correct attribute.

So, yours should look like this.

If you'd like to pause the video here, and make sure that yours looks like this, resume when you're done, and we're going to test out these getter methods.

Okay, let's test them out.

So head back to your main.

py file, and now what we're going to do is we're going to, underneath this line, we're going to print whatever comes back from one of our getter methods.

So I'm going to say, my_cat right, which is the name of my object.

And then I'm going to use a dot, and then I'm going to call the method, and let's get_name not get_species, So, my_cat.

get_name now rather than just calling that, because nothing will happen, instead I'm going to print the result of this.

So I'm going to say, print then let's run, let's see what happens.

Awesome, Fluffy! Next, I have a bit of a challenge for you.

First, and I'll need you to test all three of your getter methods, to make sure that they're working.

And then I'd like you to add a brand new method, to the Pet class that describes the animals.

So instead of having to use each of the individual methods, I want one method, that's going to print the name, tell me what species is, and also tell me the description.

Head up to your worksheet, all the instructions are there, come back when you're done and we'll go through it.

Welcome back.

How did you get on? Hopefully you're able to test your get methods, and write a new, describer method, inside of the class.

Let's have a look at my implementation of it.

Now, there are lots of different ways you could do this but this is the way that I've chosen to.

So, underneath my getters, so these are up here, I've created a new method and I've called it, "describe," again, I make sure that it has that "self" parameter, because all methods inside of a class need the "self" parameter.

Then I'm going to print a statement.

Now I'm using the "%" way of concatenating strings and inserting variables into it.

If you used the "+" symbol or "." format, whichever way you've been taught to, that's absolutely fine, as long as the end result looks the same.

Then I've inserted my three attributes, name, species and description into the sentence, and you can see the result up here.

If I head back to my main.

py file, you can see I've used the variable name and then called ".

describe." I don't need a print statement anymore, because inside of my describe method, I am actually printing.

So I don't need to print anything, because the described method will do it for me.

Now, if you want to pause the video here and use mine as an example, to get yours working, that's absolutely fine, pause the video and then continue when you're done and we'll keep going.

Right, hopefully we're all together now, some of you probably just watch me pause for three seconds, but that's fine, we're all at the same point.

Let's go on to the next activity.

We're nearly there, our class is nearly complete.

The last set of methods that we need to make are called set methods.

Now we've just made getters, these are called setters.

Let's head back to my Python repl and see what they are.

Right, we created some methods to allow us to access and get the attributes, but the might also be instances where you would like to say change some of the variables after an object has been made, and to do that we need some set methods.

So we're going to add the set methods to our class now.

I'm going to do that right at the bottom, so I'm going to head down below this described method, and hit Enter a couple of times, and then make sure that I'm lined up, with all the other "defs." And then I'm going to define it, just like any other method.

So I'm going to say, "def" and I'm going to call it "set_name" this time.

So again, I'm using the attribute name, so that's really clear, this is another convention, right? It's really clear which attribute is accessed with which get and which set method.

Then inside my brackets, of course, I'm going to have the "self" parameter.

You're going to get sick of me saying it, but that always needs to be a "self" parameter.

And then I also want another one for the set methods, because I want to change the variable value.

So I'm going to have another variable called "name," and this is going to be passed into the method, from my main.

py file.

So, then a colon, just like every other method, and then I'm going to set the "self.

name" attribute equal to the name of just been passed through, right? So this name here is the same as this one, and then I'm setting the attribute.

So you'll notice it's very similar to what we did in the constructor.

So, I'm taking a name variable in and setting my "self.

name." Now, let's have a little look at this, before I sent you off to go do your set methods.

Let's have a look at how this works.

So, my cat was originally called Fluffy.

Let's say I wanted to change the name, now I would never do this to my actual cats, but as an example, I'm going to show you how I would change it.

So I'd say my_cat.

set_name and then I want to give it a new name, and my new name is going to be "Whiskers" right, a classic name for a cat.

Now, even though I've set Fluffy as my_name in the original constructor, when I call this describe method here, we should see that I get "Whiskers" out now because that variable has been changed, let's have a look.

So Whiskers is a cat, black and white with long hair, and let's just do another name, I'm going to call the last one, is I'm going to call it "Reginald," very regal name for a cat.

And let's run that again, just we can see, that "self.

name" attribute is changing each time, and I haven't accessed it directly, I'm just using my setter methods.

So, now I'd like you to do, is head back to your worksheet, and there's a few challenges on that.

First, I'd like you to add set methods, the name, and species, and description.

I'd then like you to make three pets, so you've already made one, so you're going to have to make two more, use your get and set methods, and also call describe.

This is just practising using these objects-oriented principles.

When you're done that, resume the video.

Well done! This is the end of "Lesson 2 of Object-Oriented Programming" you've done so well to get through this.

Today, you practised creating a class of your own, using your constructor to create objects, you created get and set methods for a class, and you created an individual method called, describe.

You're really programming in an object-oriented style now.

The last thing to do, before you go, is make sure that you do the exit quiz, is going to test you, on a few of these new vocab, these new terms, we've introduced.

It's really important that you understand these, because as we continue, I'm going to start using them more and more.

If you'd like to, please ask your parent or carer to share your work on Instagram, Facebook, or Twitter, tagging out @OakNational and #LearnwithOak.

I would love to see your pets.

Well done again, and thank you for joining me, until next time, have a great day!.