1 00:00:00,570 --> 00:00:01,500 ‫Welcome back. 2 00:00:01,530 --> 00:00:04,260 ‫In the last video, you saw how to create classes. 3 00:00:04,260 --> 00:00:07,390 ‫And in this video, we're going to have a look at constructors. 4 00:00:07,410 --> 00:00:13,710 ‫As I told you earlier, that they can really improve the amount of code that you need, so you don't 5 00:00:13,710 --> 00:00:14,730 ‫need that much code. 6 00:00:14,730 --> 00:00:20,190 ‫For example, what we have written here is something that we can decrease by a lot. 7 00:00:20,190 --> 00:00:27,210 ‫So we can decrease those or we can even get rid of those two lines for each object so we could put them 8 00:00:27,210 --> 00:00:30,300 ‫into the human when we initialize it. 9 00:00:30,750 --> 00:00:32,340 ‫So let's just do that. 10 00:00:32,490 --> 00:00:37,380 ‫Let's create a new method here, which is called a constructor. 11 00:00:37,380 --> 00:00:38,730 ‫It's very close to a method. 12 00:00:38,730 --> 00:00:40,050 ‫It's not really a method. 13 00:00:40,200 --> 00:00:43,170 ‫It's a constructor which is also a member, right? 14 00:00:43,170 --> 00:00:53,550 ‫So let's create a constructor and it's public and it has the same name as the class itself. 15 00:00:53,670 --> 00:00:57,540 ‫But as you can see here, there is no void, there's no string, there's no return value. 16 00:00:57,540 --> 00:00:58,450 ‫It doesn't need it. 17 00:00:58,590 --> 00:01:01,410 ‫It just needs the access modifier. 18 00:01:01,410 --> 00:01:03,480 ‫And in this case, it's the public. 19 00:01:03,480 --> 00:01:05,100 ‫And then you need human. 20 00:01:05,100 --> 00:01:10,170 ‫So you should use public because otherwise you cannot use this constructor. 21 00:01:10,170 --> 00:01:12,180 ‫So it's really important to use public here. 22 00:01:12,210 --> 00:01:20,430 ‫Then in here you can add the parameters that you want to have each time that you create a object of 23 00:01:20,430 --> 00:01:21,270 ‫this class. 24 00:01:21,270 --> 00:01:30,480 ‫So let's say I want to have a string first name and a string last name. 25 00:01:30,930 --> 00:01:35,850 ‫So each time somebody creates a human, he needs to give me this information. 26 00:01:35,850 --> 00:01:42,600 ‫So let's have a look here as we can see in a second, as soon as it understands that there is a constructor, 27 00:01:42,600 --> 00:01:47,010 ‫it's complaining because it says that there are arguments missing. 28 00:01:47,010 --> 00:01:52,860 ‫So there is no argument given that corresponds to required formal parameter, first name of human. 29 00:01:52,860 --> 00:01:54,780 ‫So it needs two strings here. 30 00:01:54,780 --> 00:01:59,820 ‫And the first one will be the name, the first name and the second one will be the last name. 31 00:01:59,820 --> 00:02:02,040 ‫But that alone will not be enough. 32 00:02:02,430 --> 00:02:09,840 ‫So let's also fix that, because as we know, this human has member variables which are, for example, 33 00:02:09,840 --> 00:02:15,060 ‫first name and last name, and we want to change those each time that we create an object. 34 00:02:15,300 --> 00:02:21,390 ‫And we do that by saying first name is equal to first name. 35 00:02:21,810 --> 00:02:26,550 ‫And that might sound a little confusing and it actually doesn't work because it says here, Oh, no, 36 00:02:26,760 --> 00:02:28,140 ‫I don't understand what you want. 37 00:02:28,140 --> 00:02:29,670 ‫You can assign the same thing. 38 00:02:29,670 --> 00:02:32,880 ‫You can't assign the same thing to the same thing. 39 00:02:32,880 --> 00:02:34,770 ‫So the same variable to the same variable. 40 00:02:35,070 --> 00:02:38,550 ‫So what you can do here and there are two different approaches. 41 00:02:38,550 --> 00:02:47,010 ‫So either you change this variable name to let's say my first name, and then you simply say that the 42 00:02:47,010 --> 00:02:50,340 ‫global first name is going to be my first name. 43 00:02:51,150 --> 00:02:52,170 ‫That's one way. 44 00:02:52,890 --> 00:02:54,180 ‫All right, so now it works. 45 00:02:54,750 --> 00:02:57,900 ‫Or you can use the this keyword. 46 00:02:57,900 --> 00:03:04,020 ‫So this dot last name is equal to last name. 47 00:03:05,050 --> 00:03:06,120 ‫Now it suddenly works. 48 00:03:06,120 --> 00:03:06,870 ‫Why does it? 49 00:03:06,870 --> 00:03:14,520 ‫Well, it's because this last name means that I want to have the last name of this object and it's going 50 00:03:14,520 --> 00:03:15,990 ‫to be this last name. 51 00:03:15,990 --> 00:03:20,220 ‫So the member variable and not the local one. 52 00:03:20,220 --> 00:03:25,410 ‫So this string, my first name and string last name are local variables. 53 00:03:25,410 --> 00:03:27,390 ‫Pretty much so they are parameters, yes. 54 00:03:27,390 --> 00:03:32,340 ‫But they are well used as if they were local variables. 55 00:03:32,610 --> 00:03:37,500 ‫So outside of this method or of this constructor, nobody will know this. 56 00:03:37,500 --> 00:03:42,360 ‫My first name variable, for example, it's only known in here. 57 00:03:42,690 --> 00:03:47,100 ‫So now what I do is I just say, okay, this last name. 58 00:03:47,100 --> 00:03:54,270 ‫So the one from the object should be the value that is given to me whenever a human is created. 59 00:03:54,270 --> 00:03:56,310 ‫So whenever this constructor is called. 60 00:03:56,400 --> 00:04:02,190 ‫So constructor constructors pretty much like a method that is called whenever an object is created. 61 00:04:02,550 --> 00:04:09,990 ‫So in this case, whenever this human class is used as a data type, it's going to use the constructor 62 00:04:10,590 --> 00:04:12,480 ‫and it's going to call the constructor. 63 00:04:13,170 --> 00:04:16,620 ‫So as you can see, these are the two different ways that you can do that. 64 00:04:17,220 --> 00:04:19,650 ‫So now let's go ahead and go back to the program. 65 00:04:19,650 --> 00:04:24,860 ‫And as you can see, I can simply add the values in here. 66 00:04:24,870 --> 00:04:33,900 ‫So CC and let's say Wagner and I'm going to get rid of all the other things that we have here. 67 00:04:33,900 --> 00:04:37,740 ‫So we only have CC now and CC is going to introduce herself. 68 00:04:37,740 --> 00:04:42,960 ‫So now you see we have limited the whole code or we use the whole code to just one line. 69 00:04:42,960 --> 00:04:47,400 ‫We had four lines before or three and now we just have this one line. 70 00:04:47,400 --> 00:04:48,780 ‫So we say the same thing. 71 00:04:48,780 --> 00:04:50,040 ‫So hi. 72 00:04:50,040 --> 00:04:51,180 ‫I'm CC Wagner. 73 00:04:51,360 --> 00:04:52,380 ‫So just. 74 00:04:53,490 --> 00:04:57,030 ‫Hand over the first name and hands over the last name. 75 00:04:57,030 --> 00:05:00,360 ‫So if you hover over human, you can see what it needs. 76 00:05:00,360 --> 00:05:03,390 ‫It needs string my first name and string last name. 77 00:05:04,020 --> 00:05:07,770 ‫And if we look at our human class, that's exactly what we have here. 78 00:05:08,910 --> 00:05:12,060 ‫So now you see how you can use a constructor. 79 00:05:12,420 --> 00:05:20,610 ‫And I'm going to actually change that to first name and I'm going to also use this and here it should 80 00:05:20,610 --> 00:05:24,060 ‫be also first name because I like to use this. 81 00:05:24,060 --> 00:05:30,900 ‫So I always know I'm using the field or I'm using the member variable, which is the global variable, 82 00:05:30,900 --> 00:05:31,380 ‫right. 83 00:05:31,800 --> 00:05:36,390 ‫By the way, now we don't need to use public anymore so we can make it private. 84 00:05:36,690 --> 00:05:38,820 ‫We can make both variables private. 85 00:05:38,820 --> 00:05:43,880 ‫Now, just so you see that it's still working, let's run the code again and you see. 86 00:05:43,890 --> 00:05:45,080 ‫Hi, I'm sick, Wagner. 87 00:05:45,180 --> 00:05:50,520 ‫So now I don't actually access those variables from here anymore. 88 00:05:50,520 --> 00:05:58,380 ‫I simply create a constructor or create a human object, and I deliver the constructor or I call the 89 00:05:58,380 --> 00:06:01,260 ‫constructor by handing over this information. 90 00:06:01,260 --> 00:06:05,550 ‫And I need to, because otherwise the whole object won't be created. 91 00:06:05,550 --> 00:06:08,670 ‫It's going to result in an error, as you could see. 92 00:06:08,670 --> 00:06:10,920 ‫So here now it's it doesn't like the code. 93 00:06:10,920 --> 00:06:14,970 ‫It says, oh, there is something missing, there is a constructor information missing. 94 00:06:14,970 --> 00:06:16,590 ‫So this is no argument. 95 00:06:16,770 --> 00:06:20,940 ‫There is no argument given that corresponds to the required form parameter last name. 96 00:06:21,330 --> 00:06:21,690 ‫All right. 97 00:06:21,690 --> 00:06:24,240 ‫So I'm just going to give it the last name again. 98 00:06:24,240 --> 00:06:26,030 ‫And what was this? 99 00:06:26,970 --> 00:06:29,460 ‫Her name was I'm just going to call it Wagner. 100 00:06:29,460 --> 00:06:34,050 ‫So now she married the other guy and now actually she was Wagner. 101 00:06:34,050 --> 00:06:34,530 ‫Right. 102 00:06:34,800 --> 00:06:35,320 ‫Okay. 103 00:06:35,490 --> 00:06:44,400 ‫So coming back to our constructor and our variables here, a little challenge for you. 104 00:06:45,300 --> 00:06:46,590 ‫So here's the challenge. 105 00:06:46,620 --> 00:06:48,930 ‫Add two more member variables to human. 106 00:06:48,930 --> 00:06:50,190 ‫One is called eye color. 107 00:06:50,190 --> 00:06:51,250 ‫The other one H. 108 00:06:51,360 --> 00:06:56,640 ‫You should use the correct data types here, then adjust the constructor so it requires all the four 109 00:06:56,640 --> 00:06:59,730 ‫values to be used when creating an object of humans. 110 00:06:59,730 --> 00:07:06,630 ‫So it should use fast name, last name, eye color and age, and then create two objects of type human. 111 00:07:06,720 --> 00:07:10,230 ‫And of course, you need to create them within our program class. 112 00:07:10,830 --> 00:07:11,160 ‫All right. 113 00:07:11,160 --> 00:07:13,740 ‫So go ahead and post the video and try to do that. 114 00:07:15,840 --> 00:07:16,230 ‫All right. 115 00:07:16,230 --> 00:07:17,460 ‫I hope you try to do that. 116 00:07:17,460 --> 00:07:26,520 ‫So let's go ahead and first of all, create a public string I or actually private string I color and 117 00:07:26,520 --> 00:07:32,730 ‫then private H, which is a type int. 118 00:07:33,210 --> 00:07:35,370 ‫And now let's adjust the constructor. 119 00:07:35,970 --> 00:07:46,620 ‫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 120 00:07:46,860 --> 00:07:48,990 ‫content of the constructor as well. 121 00:07:48,990 --> 00:07:58,020 ‫So I color is going to be color and this the H is going to be H. 122 00:07:58,620 --> 00:08:01,890 ‫So now let's go back here and let's create an object. 123 00:08:02,710 --> 00:08:05,380 ‫Are actually two objects so human. 124 00:08:05,410 --> 00:08:12,460 ‫In my case, it's going to be Dennis is a new human and I'm going to enter the first name. 125 00:08:13,870 --> 00:08:15,190 ‫Last name. 126 00:08:15,550 --> 00:08:19,440 ‫And as you can see here, A.J. tells us what we need to enter. 127 00:08:19,450 --> 00:08:21,680 ‫So first name, last name, eye color H. 128 00:08:21,700 --> 00:08:23,440 ‫All right, so let's do that. 129 00:08:23,440 --> 00:08:30,160 ‫Eye color is green and H is an integer. 130 00:08:30,160 --> 00:08:32,890 ‫So we just enter 29 in my case. 131 00:08:32,890 --> 00:08:36,010 ‫And now I want Dennis to introduce himself. 132 00:08:36,640 --> 00:08:40,210 ‫And the thing is, introduce myself has not been adjusted yet. 133 00:08:40,210 --> 00:08:45,970 ‫So if I run that, it's not going to add the great information about eye color and age yet. 134 00:08:45,970 --> 00:09:01,780 ‫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 135 00:09:01,780 --> 00:09:13,600 ‫that will be our to my eye color is and this is going to be our three, which is the fourth value. 136 00:09:13,810 --> 00:09:22,150 ‫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 137 00:09:22,150 --> 00:09:24,400 ‫that I've created the variables here. 138 00:09:24,490 --> 00:09:27,810 ‫So now if we run the code again, we will see. 139 00:09:27,820 --> 00:09:30,880 ‫Hi, I'm Dennis Panetta and 29 years old. 140 00:09:30,880 --> 00:09:32,380 ‫My eye color is green. 141 00:09:32,830 --> 00:09:35,350 ‫Very useful information. 142 00:09:35,350 --> 00:09:36,160 ‫Thank you very much. 143 00:09:36,160 --> 00:09:42,520 ‫So now let's create this second one human and I'm going to call it Amalia, which is my daughter. 144 00:09:42,520 --> 00:09:45,550 ‫She's a new human and she's pretty new. 145 00:09:45,580 --> 00:09:50,680 ‫Amalia, new to her eye color is still blue. 146 00:09:51,520 --> 00:09:55,780 ‫Babies have tend to have blue eyes if the parents don't have brown. 147 00:09:55,780 --> 00:09:57,940 ‫And now how old is she? 148 00:09:57,940 --> 00:09:59,500 ‫She's one year old. 149 00:09:59,920 --> 00:10:02,980 ‫And now, Amaya, come on, introduce yourself, please. 150 00:10:03,040 --> 00:10:09,970 ‫Unfortunately, she's not communicative enough or she doesn't know how to speak yet, at least not this 151 00:10:09,970 --> 00:10:10,960 ‫kind of information. 152 00:10:10,960 --> 00:10:14,830 ‫So that we are high, in my opinion, to end one years old. 153 00:10:14,830 --> 00:10:16,780 ‫So that's something that you could even adjust. 154 00:10:16,780 --> 00:10:20,110 ‫So you could create an if statement and introduce yourself. 155 00:10:20,110 --> 00:10:27,580 ‫So if somebody is below so-and-so and so, so below two years, so actually one and zero years or I'm 156 00:10:27,580 --> 00:10:32,500 ‫not sure whether with zero it works, but for one year you could say, okay, it's not years, but it's 157 00:10:32,500 --> 00:10:33,490 ‫going to be here. 158 00:10:34,150 --> 00:10:34,750 ‫You know what? 159 00:10:34,750 --> 00:10:36,190 ‫I actually try to do that. 160 00:10:38,020 --> 00:10:38,320 ‫All right. 161 00:10:38,320 --> 00:10:39,250 ‫I hope you tried it. 162 00:10:39,250 --> 00:10:42,520 ‫So I'm just going to use a little if statement here. 163 00:10:44,300 --> 00:10:56,000 ‫So if age is equal to one, then do something else. 164 00:10:58,010 --> 00:10:59,450 ‫And otherwise do that. 165 00:11:01,360 --> 00:11:06,070 ‫So if age is or you're actually one year old. 166 00:11:08,040 --> 00:11:11,340 ‫And else to this. 167 00:11:12,750 --> 00:11:13,350 ‫All right. 168 00:11:13,620 --> 00:11:14,790 ‫And now it should be fine. 169 00:11:14,880 --> 00:11:15,960 ‫So there we are. 170 00:11:16,050 --> 00:11:17,190 ‫Hi, I'm Amaya. 171 00:11:17,190 --> 00:11:18,300 ‫And one year old. 172 00:11:18,300 --> 00:11:19,440 ‫My color is blue. 173 00:11:19,650 --> 00:11:20,330 ‫Great. 174 00:11:20,340 --> 00:11:22,800 ‫So I hope you manage to do that. 175 00:11:22,800 --> 00:11:25,110 ‫And now you see how to use constructors. 176 00:11:25,500 --> 00:11:32,160 ‫Well, a constructor, because in the next video we are going to use constructors plural, which means 177 00:11:32,160 --> 00:11:38,040 ‫we are going to create multiple constructors in the same class and you will see what the purpose of 178 00:11:38,040 --> 00:11:40,500 ‫that is and why that is useful and important. 179 00:11:40,500 --> 00:11:47,460 ‫And we will use many classes in the future which have multiple different constructors. 180 00:11:47,460 --> 00:11:49,320 ‫So see you in the next video.