‫Welcome back.
‫In the last video, you saw how to create classes.
‫And in this video, we're going to have a look at constructors.
‫As I told you earlier, that they can really improve the amount of code that you need, so you don't
‫need that much code.
‫For example, what we have written here is something that we can decrease by a lot.
‫So we can decrease those or we can even get rid of those two lines for each object so we could put them
‫into the human when we initialize it.
‫So let's just do that.
‫Let's create a new method here, which is called a constructor.
‫It's very close to a method.
‫It's not really a method.
‫It's a constructor which is also a member, right?
‫So let's create a constructor and it's public and it has the same name as the class itself.
‫But as you can see here, there is no void, there's no string, there's no return value.
‫It doesn't need it.
‫It just needs the access modifier.
‫And in this case, it's the public.
‫And then you need human.
‫So you should use public because otherwise you cannot use this constructor.
‫So it's really important to use public here.
‫Then in here you can add the parameters that you want to have each time that you create a object of
‫this class.
‫So let's say I want to have a string first name and a string last name.
‫So each time somebody creates a human, he needs to give me this information.
‫So let's have a look here as we can see in a second, as soon as it understands that there is a constructor,
‫it's complaining because it says that there are arguments missing.
‫So there is no argument given that corresponds to required formal parameter, first name of human.
‫So it needs two strings here.
‫And the first one will be the name, the first name and the second one will be the last name.
‫But that alone will not be enough.
‫So let's also fix that, because as we know, this human has member variables which are, for example,
‫first name and last name, and we want to change those each time that we create an object.
‫And we do that by saying first name is equal to first name.
‫And that might sound a little confusing and it actually doesn't work because it says here, Oh, no,
‫I don't understand what you want.
‫You can assign the same thing.
‫You can't assign the same thing to the same thing.
‫So the same variable to the same variable.
‫So what you can do here and there are two different approaches.
‫So either you change this variable name to let's say my first name, and then you simply say that the
‫global first name is going to be my first name.
‫That's one way.
‫All right, so now it works.
‫Or you can use the this keyword.
‫So this dot last name is equal to last name.
‫Now it suddenly works.
‫Why does it?
‫Well, it's because this last name means that I want to have the last name of this object and it's going
‫to be this last name.
‫So the member variable and not the local one.
‫So this string, my first name and string last name are local variables.
‫Pretty much so they are parameters, yes.
‫But they are well used as if they were local variables.
‫So outside of this method or of this constructor, nobody will know this.
‫My first name variable, for example, it's only known in here.
‫So now what I do is I just say, okay, this last name.
‫So the one from the object should be the value that is given to me whenever a human is created.
‫So whenever this constructor is called.
‫So constructor constructors pretty much like a method that is called whenever an object is created.
‫So in this case, whenever this human class is used as a data type, it's going to use the constructor
‫and it's going to call the constructor.
‫So as you can see, these are the two different ways that you can do that.
‫So now let's go ahead and go back to the program.
‫And as you can see, I can simply add the values in here.
‫So CC and let's say Wagner and I'm going to get rid of all the other things that we have here.
‫So we only have CC now and CC is going to introduce herself.
‫So now you see we have limited the whole code or we use the whole code to just one line.
‫We had four lines before or three and now we just have this one line.
‫So we say the same thing.
‫So hi.
‫I'm CC Wagner.
‫So just.
‫Hand over the first name and hands over the last name.
‫So if you hover over human, you can see what it needs.
‫It needs string my first name and string last name.
‫And if we look at our human class, that's exactly what we have here.
‫So now you see how you can use a constructor.
‫And I'm going to actually change that to first name and I'm going to also use this and here it should
‫be also first name because I like to use this.
‫So I always know I'm using the field or I'm using the member variable, which is the global variable,
‫right.
‫By the way, now we don't need to use public anymore so we can make it private.
‫We can make both variables private.
‫Now, just so you see that it's still working, let's run the code again and you see.
‫Hi, I'm sick, Wagner.
‫So now I don't actually access those variables from here anymore.
‫I simply create a constructor or create a human object, and I deliver the constructor or I call the
‫constructor by handing over this information.
‫And I need to, because otherwise the whole object won't be created.
‫It's going to result in an error, as you could see.
‫So here now it's it doesn't like the code.
‫It says, oh, there is something missing, there is a constructor information missing.
‫So this is no argument.
‫There is no argument given that corresponds to the required form parameter last name.
‫All right.
‫So I'm just going to give it the last name again.
‫And what was this?
‫Her name was I'm just going to call it Wagner.
‫So now she married the other guy and now actually she was Wagner.
‫Right.
‫Okay.
‫So coming back to our constructor and our variables here, a little challenge for you.
‫So here's the challenge.
‫Add two more member variables to human.
‫One is called eye color.
‫The other one H.
‫You should use the correct data types here, then adjust the constructor so it requires all the four
‫values to be used when creating an object of humans.
‫So it should use fast name, last name, eye color and age, and then create two objects of type human.
‫And of course, you need to create them within our program class.
‫All right.
‫So go ahead and post the video and try to do that.
‫All right.
‫I hope you try to do that.
‫So let's go ahead and first of all, create a public string I or actually private string I color and
‫then private H, which is a type int.
‫And now let's adjust the constructor.
‫So I'm going to add a string I color and I'm going to add an int H and I'm just going to adjust the
‫content of the constructor as well.
‫So I color is going to be color and this the H is going to be H.
‫So now let's go back here and let's create an object.
‫Are actually two objects so human.
‫In my case, it's going to be Dennis is a new human and I'm going to enter the first name.
‫Last name.
‫And as you can see here, A.J. tells us what we need to enter.
‫So first name, last name, eye color H.
‫All right, so let's do that.
‫Eye color is green and H is an integer.
‫So we just enter 29 in my case.
‫And now I want Dennis to introduce himself.
‫And the thing is, introduce myself has not been adjusted yet.
‫So if I run that, it's not going to add the great information about eye color and age yet.
‫So let's go back to introduce myself and add that I'm so-and-so and so-and-so old, so years old, and
‫that will be our to my eye color is and this is going to be our three, which is the fourth value.
‫So I color and H so I'm going to add it in the order that I have used here in my text, not in the order
‫that I've created the variables here.
‫So now if we run the code again, we will see.
‫Hi, I'm Dennis Panetta and 29 years old.
‫My eye color is green.
‫Very useful information.
‫Thank you very much.
‫So now let's create this second one human and I'm going to call it Amalia, which is my daughter.
‫She's a new human and she's pretty new.
‫Amalia, new to her eye color is still blue.
‫Babies have tend to have blue eyes if the parents don't have brown.
‫And now how old is she?
‫She's one year old.
‫And now, Amaya, come on, introduce yourself, please.
‫Unfortunately, she's not communicative enough or she doesn't know how to speak yet, at least not this
‫kind of information.
‫So that we are high, in my opinion, to end one years old.
‫So that's something that you could even adjust.
‫So you could create an if statement and introduce yourself.
‫So if somebody is below so-and-so and so, so below two years, so actually one and zero years or I'm
‫not sure whether with zero it works, but for one year you could say, okay, it's not years, but it's
‫going to be here.
‫You know what?
‫I actually try to do that.
‫All right.
‫I hope you tried it.
‫So I'm just going to use a little if statement here.
‫So if age is equal to one, then do something else.
‫And otherwise do that.
‫So if age is or you're actually one year old.
‫And else to this.
‫All right.
‫And now it should be fine.
‫So there we are.
‫Hi, I'm Amaya.
‫And one year old.
‫My color is blue.
‫Great.
‫So I hope you manage to do that.
‫And now you see how to use constructors.
‫Well, a constructor, because in the next video we are going to use constructors plural, which means
‫we are going to create multiple constructors in the same class and you will see what the purpose of
‫that is and why that is useful and important.
‫And we will use many classes in the future which have multiple different constructors.
‫So see you in the next video.