Welcome back.
In this video we're going to look at methods but we're going to keep it very simple okay.
In the next video we're going to go into methods a little more complex.
But in this case I just want to really give you an idea as to what methods can do.
And this will be one of the examples.
There are multiple examples necessary in order to understand that the depth of methods.
So I'm inside of this car class.
And you see I have a customer class here.
We're going to look at that later.
But for now in the car class we have our member variables.
We have our properties and then we have our constructor.
So nothing else at this point.
But what we can do is we can add methods to our car.
And a method that is added to a class modifies what the object of that class can do.
Okay.
So for example, what would be something that a car can do?
A car can drive okay.
So let's go ahead and create a public void drive method.
So this will be a super simple method.
The only thing that it will do is I'm driving.
Is it what it's going to say okay.
So really really simple.
So we are using the public keyword which allows us to make sure that we can use this method outside
of this class.
So we can use it inside of the program class for example.
Then we use void which means we're not going to return anything.
And we use the name drive.
So capital letter.
And then just the normal way you would write drive and then the brackets here you could add parameters.
But in this case I'm going to keep it extremely simple.
And I'm just going to say okay now let's say I'm driving okay.
Now let's go over here.
Now let's go over to our program CSS file.
And I cleaned it up a little bit.
I've gotten rid of all of the other code.
But what I'm going to do is I'm going to create a new car.
So my Audi is a new car like so now we look at car and we need to add the model brand and is luxury
right.
We don't have any other constructors yet.
No we don't okay so let's add the model.
It will be A3.
The brand will be Audi and his luxury will be false.
So we're not using the luxury version.
And now what I'm going to do is I'm going to use my Audi.
And then I'm going to call the drive method like so.
So you can see that you can use a method on top of a class, which is what we've been doing here all
the time.
Right.
We've used console dot read key or with our key w we used console dot write line.
So we also used a method that is inside of a class.
It's a little different here because we didn't need to create an object of console in order to do that.
And that's why this write line is static.
So then we come back to the static keyword, which we haven't talked about at all.
But now it starts to make sense.
I'm going to go over this keyword a little later.
But what we do have now here is an object that we created.
And we can use its method so its functionality to do something.
So we see that an object can have properties and it can have methods.
So the properties are attributes of that object.
And the methods are its capabilities, its skills, its things that it can do.
So for example it can drive.
These methods are used to actually do something okay.
So to execute a bit of code and actually run something like for example, drive.
So now my Audi is going to drive.
So let me run that and we will see that.
It just says I'm driving.
First of all an Audi A3 has been created.
Yeah that's the other line that we had.
And then it says I'm driving.
So what I can do now is I can give this method drive some more information.
So for example, I could say I'm a so and so and I'm driving.
So let's add a dollar sign here to use string manipulation.
And I'm going to say I'm.
And then I'm going to use a model and I'm driving or even more we shouldn't use model like this.
We should use the property okay.
So we're going to use the property here because in the property we define what should happen if we want
to access the model.
Right.
So here we are accessing the model.
And I'm saying I'm a model and I'm driving.
So let's run the same code again.
And we see I'm an A3 and I'm driving I'm a A3 A3.
Well grammatically not correct but still fine.
So it says I'm a A3 and I'm driving so it is aware this.
Object is self-aware now, so whenever it is driving, it knows which object it is.
And now comes the crazy part.
Let's look at that.
Let's create another car.
So car, my BMW for example, is going to be a new car and it will be an i7, i7 BMW and it's going
to be a luxury car.
So I'm going to say true here okay.
And now I'm going to say my BMW dot drive.
So now let's run this again.
And now it says I'm a i7 and I'm driving.
So first the Audi is driving and then the BMW is driving.
So even though I'm using this drive method, it's the same method, right.
But now it behaves differently.
It gives us a different output.
Because what this drive method does is it allows us to use the functionality of our blueprint.
So of our car blueprint where we defined what the car is capable of, what kind of attributes it has,
so what properties it has so to speak, and which capabilities such as driving or stopping braking,
I don't know, like repairing or whatever it has.
And now we can use its methods that will then be specific to the object itself, or will hold some information
about the object or whatever you want it to do.
Basically, you have full control now, and we're going to look at that in more depth in the next video
where we're going to use that with the customer as well.
Okay.
So the customer class, which will be a different class, but let's do that and then get an even better
understanding of what methods are capable of, especially methods that are not just there to allow us
to use a chunk of code multiple times, but now actually use that chunk of code that we are building
inside of our blueprint.
Do something based on the object where it is used from or where it is used on, so to speak.
So we're using it on the object of BMW.
We're calling the drive method.
And it gives us a different result than doing the same thing on the Audi object okay.
So that's it for this video.
See you in the next one.