1 00:00:00,990 --> 00:00:01,950 ‫Welcome back. 2 00:00:01,980 --> 00:00:07,260 ‫In this video, we are going to have a look at the different amounts of constructors that we can create. 3 00:00:07,260 --> 00:00:15,480 ‫And they are limitless, almost at least, at least as much as you have space in your memory, but you 4 00:00:15,480 --> 00:00:17,340 ‫can create plenty of constructors. 5 00:00:17,340 --> 00:00:19,090 ‫So that's the general idea. 6 00:00:19,110 --> 00:00:26,730 ‫We have created a constructor here in our last video and that constructor is a parameterized constructor. 7 00:00:26,790 --> 00:00:30,150 ‫Parameterized constructor. 8 00:00:30,270 --> 00:00:33,390 ‫Now we don't need to create a parameterized constructor. 9 00:00:33,390 --> 00:00:36,720 ‫We can even create a non parameter write constructor. 10 00:00:36,720 --> 00:00:39,120 ‫So a default constructor. 11 00:00:39,120 --> 00:00:45,540 ‫So let's create a default constructor at the top and I'm going to call it public human. 12 00:00:46,440 --> 00:00:50,970 ‫And in here I'm just going to write something on the console. 13 00:00:50,970 --> 00:01:00,780 ‫So see W constructor called or you could even say object created. 14 00:01:01,860 --> 00:01:04,500 ‫So that's pretty much it, nothing else than that. 15 00:01:04,500 --> 00:01:10,320 ‫So now if I do that, as you can see, we have a parameterized constructor and now we have a default 16 00:01:10,320 --> 00:01:11,580 ‫constructor as well. 17 00:01:11,760 --> 00:01:13,230 ‫Default constructor. 18 00:01:13,680 --> 00:01:20,730 ‫We can create an object of type human that doesn't have any further information yet. 19 00:01:20,760 --> 00:01:26,790 ‫So human and I'm going to call that basic human is going to be a new human. 20 00:01:27,390 --> 00:01:28,110 ‫That's it. 21 00:01:28,410 --> 00:01:31,050 ‫So now basic human doesn't know how to reduce themselves. 22 00:01:31,080 --> 00:01:39,570 ‫Of course we can call that method basic human, introduce myself, but there won't be much information 23 00:01:39,600 --> 00:01:40,320 ‫given. 24 00:01:40,620 --> 00:01:45,110 ‫So if we call that, we say constructor called object created. 25 00:01:45,120 --> 00:01:48,000 ‫Hi, I'm zero years old. 26 00:01:48,000 --> 00:01:55,170 ‫My color is so there is no information given because this basic human well as I said, it doesn't have 27 00:01:55,170 --> 00:01:56,550 ‫any further information. 28 00:01:56,700 --> 00:02:02,160 ‫So as you can see, we have already two different constructors, but we can even create more. 29 00:02:02,160 --> 00:02:05,460 ‫So we could even go ahead and copy that one here. 30 00:02:06,570 --> 00:02:09,150 ‫So let's copy the constructor that we had earlier. 31 00:02:09,150 --> 00:02:13,980 ‫Let's say we don't have the age, so we only have the other information. 32 00:02:14,970 --> 00:02:19,440 ‫Now we could go ahead and create a human which doesn't have the age. 33 00:02:20,170 --> 00:02:23,590 ‫So let's say we don't have the age of Somalia. 34 00:02:24,370 --> 00:02:29,110 ‫We can get rid of that and we can run the code again. 35 00:02:29,800 --> 00:02:33,940 ‫So now it says, Hi, I'm Somalia and zero years old. 36 00:02:33,940 --> 00:02:42,370 ‫My color is blue and it writes zero because if an empty value is empty, like in our case H an integer 37 00:02:42,370 --> 00:02:43,180 ‫is empty. 38 00:02:43,210 --> 00:02:45,580 ‫It's just going to be default to zero. 39 00:02:45,580 --> 00:02:50,200 ‫So H is simply initialized to zero even if we don't initialize it. 40 00:02:50,200 --> 00:02:56,140 ‫So as you can see here, it says zero years old, even though we didn't enter anything. 41 00:02:57,640 --> 00:03:00,500 ‫So how can we solve that? 42 00:03:00,520 --> 00:03:01,640 ‫How can we fix that? 43 00:03:01,660 --> 00:03:09,580 ‫We could fix the whole introduce myself thing so we could say, okay, if somebody is or if there is 44 00:03:09,580 --> 00:03:10,630 ‫any age. 45 00:03:10,630 --> 00:03:15,130 ‫So let's say if age is not equal, zero. 46 00:03:16,000 --> 00:03:18,610 ‫Go ahead and write this one here. 47 00:03:19,570 --> 00:03:21,640 ‫I'm going to change this up a lot here. 48 00:03:21,640 --> 00:03:23,410 ‫So I'm gonna change it. 49 00:03:23,410 --> 00:03:24,850 ‫So if age is not equal. 50 00:03:24,850 --> 00:03:25,600 ‫Zero, right? 51 00:03:25,600 --> 00:03:32,610 ‫That and ls only introduce yourself with the first name, last name and color. 52 00:03:33,130 --> 00:03:39,340 ‫So get rid of H here and get rid of. 53 00:03:41,210 --> 00:03:44,630 ‫This one here with the years old. 54 00:03:44,810 --> 00:03:46,520 ‫So now let's run this again. 55 00:03:48,090 --> 00:03:50,160 ‫And we actually get an error. 56 00:03:50,850 --> 00:03:53,520 ‫Let me check, because I didn't change that value. 57 00:03:53,740 --> 00:03:54,010 ‫Okay. 58 00:03:54,030 --> 00:03:55,230 ‫Now we can run it again. 59 00:03:55,320 --> 00:03:55,950 ‫Now we see. 60 00:03:56,020 --> 00:03:56,580 ‫I'm Dennis. 61 00:03:56,580 --> 00:03:57,010 ‫Blah, blah. 62 00:03:57,060 --> 00:03:57,700 ‫That's fine. 63 00:03:57,720 --> 00:03:59,050 ‫I'm Amalia Panetta. 64 00:03:59,070 --> 00:04:00,210 ‫My eye color's blue. 65 00:04:00,240 --> 00:04:07,440 ‫So now amalia because there was no age given, is used or treated differently in a way that she introduces 66 00:04:07,440 --> 00:04:08,670 ‫herself differently. 67 00:04:08,880 --> 00:04:10,490 ‫And why is that useful? 68 00:04:10,500 --> 00:04:11,790 ‫Why would we have that? 69 00:04:11,970 --> 00:04:19,380 ‫Let's say you want to write a program which takes user data from a database and then it should do something 70 00:04:19,380 --> 00:04:19,980 ‫with that data. 71 00:04:19,980 --> 00:04:25,710 ‫So it should do some calculation or it should write some information to them or whatever. 72 00:04:25,920 --> 00:04:30,060 ‫So if you have all the information about that user, that's fine. 73 00:04:30,060 --> 00:04:34,860 ‫Then you can create a form and you can add all the information into that form and you can print it out 74 00:04:34,860 --> 00:04:37,250 ‫and send it to the person or whatever. 75 00:04:37,260 --> 00:04:41,370 ‫But what if you are missing some information? 76 00:04:41,370 --> 00:04:42,960 ‫So let's say you don't have the age. 77 00:04:42,960 --> 00:04:47,610 ‫Well, then you will simply not write the age onto the form. 78 00:04:47,610 --> 00:04:51,270 ‫But what if there is something more important missing like the address? 79 00:04:51,270 --> 00:04:52,740 ‫Why would you even print it out? 80 00:04:52,740 --> 00:04:54,630 ‫Because you cannot send it to that person. 81 00:04:54,630 --> 00:04:57,090 ‫So then you would write a different type of code. 82 00:04:57,090 --> 00:05:02,760 ‫So you could say, okay, I am collecting all the people that don't have an address and I'm doing something 83 00:05:02,760 --> 00:05:03,000 ‫else. 84 00:05:03,000 --> 00:05:06,110 ‫I'm checking Facebook for their name or something like that. 85 00:05:06,120 --> 00:05:08,810 ‫So the program would then check the Facebook API. 86 00:05:08,820 --> 00:05:11,880 ‫Is there a person with that first name and last name and age? 87 00:05:11,880 --> 00:05:17,040 ‫And then Facebook would give the information that the program could run some further investigation on 88 00:05:17,040 --> 00:05:17,700 ‫that person. 89 00:05:17,700 --> 00:05:20,880 ‫And that's some data sensitive stuff that I'm talking about here. 90 00:05:20,880 --> 00:05:28,860 ‫But in general, it's very useful to create constructors for with different information. 91 00:05:28,860 --> 00:05:34,410 ‫So we have one with four parameters, one with three parameters. 92 00:05:34,410 --> 00:05:37,380 ‫We can still create one with two or one with one. 93 00:05:37,380 --> 00:05:39,230 ‫So that's a little challenge for you. 94 00:05:39,420 --> 00:05:44,250 ‫Adjust the parameters so you have all the four cases or actually five cases. 95 00:05:44,250 --> 00:05:45,840 ‫So this is the default constructor. 96 00:05:45,840 --> 00:05:52,980 ‫Now please go ahead and create a different constructor for first name and last name, another one for 97 00:05:52,980 --> 00:05:54,030 ‫only first name. 98 00:05:54,030 --> 00:05:57,810 ‫And then of course adjust to introduce myself method accordingly. 99 00:06:00,630 --> 00:06:01,020 ‫All right. 100 00:06:01,020 --> 00:06:02,240 ‫I hope you tried it. 101 00:06:02,250 --> 00:06:06,300 ‫So what I'm going to do is I'm just going to copy that code here. 102 00:06:06,480 --> 00:06:08,490 ‫I'm going to get rid of the eye color. 103 00:06:11,250 --> 00:06:11,900 ‫That's it. 104 00:06:11,910 --> 00:06:14,210 ‫So that's the other constructor. 105 00:06:14,220 --> 00:06:17,940 ‫Now I'm going to copy that and I'm going to get rid of the last name. 106 00:06:18,570 --> 00:06:19,170 ‫All right. 107 00:06:19,170 --> 00:06:22,090 ‫And let's get rid of last name. 108 00:06:22,110 --> 00:06:25,230 ‫And now, of course, we need to adjust our method still. 109 00:06:25,230 --> 00:06:25,890 ‫So. 110 00:06:26,690 --> 00:06:28,750 ‫They introduce myself method. 111 00:06:28,760 --> 00:06:38,030 ‫So here we had the case when H was not zero and let's say last name is not equal now. 112 00:06:39,140 --> 00:06:41,300 ‫And what else do we have here? 113 00:06:41,300 --> 00:06:52,070 ‫I color is not equal null and finally our first name so we could have a different order. 114 00:06:52,160 --> 00:06:53,990 ‫But in the end it's fine. 115 00:06:53,990 --> 00:07:02,750 ‫So if all of those values are not equal null, then I want to go ahead and do this otherwise else if 116 00:07:03,800 --> 00:07:06,170 ‫I want to have a different check here. 117 00:07:06,170 --> 00:07:16,420 ‫So I'm going to copy this if here and I'm going to say in the next one, what if we don't have an equal 118 00:07:16,430 --> 00:07:20,000 ‫so we can get rid of this one and now let's copy that again. 119 00:07:20,330 --> 00:07:26,210 ‫And now we are lacking the H in general, so let's get rid of the H. 120 00:07:26,750 --> 00:07:32,180 ‫And now finally we have a version where we don't have a last name. 121 00:07:32,570 --> 00:07:34,880 ‫So now we of course need to adjust the statements. 122 00:07:34,880 --> 00:07:43,550 ‫So if we have first name and we have last name and we have the H and here we only have H, then we want 123 00:07:43,550 --> 00:07:50,780 ‫to write something about the H and I and so on. 124 00:07:50,780 --> 00:07:51,560 ‫So old. 125 00:07:51,560 --> 00:07:54,710 ‫So two years old. 126 00:07:55,190 --> 00:07:56,990 ‫Now we don't need that anymore. 127 00:08:01,370 --> 00:08:01,580 ‫Now. 128 00:08:01,580 --> 00:08:05,630 ‫That's if we have the age, if we have the last name and the first name, right? 129 00:08:05,630 --> 00:08:12,530 ‫So now we only have first name and last name so we can get rid of the whole color aspect. 130 00:08:12,530 --> 00:08:15,710 ‫And now finally we only have the first name, right? 131 00:08:15,710 --> 00:08:18,710 ‫So let's get rid of everything else except for the first name. 132 00:08:19,040 --> 00:08:26,060 ‫So now our introduce myself method is going to be clean and it's always going to inform us about exactly 133 00:08:26,060 --> 00:08:27,050 ‫what is available. 134 00:08:27,170 --> 00:08:38,790 ‫So let's say we create a new human and not a basic one, but let's call this human Nataly, that leap. 135 00:08:39,710 --> 00:08:44,810 ‫And she is a new human and we only have her first name. 136 00:08:45,110 --> 00:08:47,510 ‫And now, Natalie, please introduce yourself. 137 00:08:47,510 --> 00:08:51,260 ‫Maybe we should not use a capital letter here. 138 00:08:51,290 --> 00:08:55,590 ‫Natalie, introduce yourself. 139 00:08:56,600 --> 00:08:59,360 ‫And now we have one which has a last name. 140 00:08:59,360 --> 00:09:00,560 ‫So human. 141 00:09:01,470 --> 00:09:03,380 ‫And I'm going to call the Michael again. 142 00:09:04,010 --> 00:09:07,490 ‫Great name, new human Michael. 143 00:09:07,700 --> 00:09:10,370 ‫And it's not a human, but it's a human. 144 00:09:10,370 --> 00:09:18,260 ‫And this Michael is going to introduce himself and Michael should have a last name. 145 00:09:18,260 --> 00:09:20,570 ‫So I'm going to call a michael Miller. 146 00:09:22,400 --> 00:09:31,040 ‫And now the last version, human and we'll be frank, is a new human. 147 00:09:31,700 --> 00:09:33,110 ‫And it's Frank. 148 00:09:33,980 --> 00:09:40,580 ‫And this guy, he is called Walter, very funny last name. 149 00:09:40,580 --> 00:09:42,530 ‫And then he has an H. 150 00:09:43,280 --> 00:09:44,990 ‫So let's say he's. 151 00:09:46,290 --> 00:09:48,150 ‫Well, actually, we don't have that version. 152 00:09:48,630 --> 00:09:52,190 ‫We don't have the version where we only have the edge, but we don't have the color. 153 00:09:52,200 --> 00:09:56,490 ‫So that would be another constructor that we could create. 154 00:09:56,520 --> 00:09:59,430 ‫Actually, let's do that just because we can. 155 00:09:59,640 --> 00:10:01,170 ‫Let's create a different one. 156 00:10:01,170 --> 00:10:02,520 ‫I'm just going to copy. 157 00:10:02,880 --> 00:10:04,650 ‫Actually, I'm going to copy that one. 158 00:10:04,650 --> 00:10:12,270 ‫The parameter right here and I'm going to put it here, but I'm going to get rid of the I color and 159 00:10:12,270 --> 00:10:14,250 ‫I'm going to get rid of eye color here as well. 160 00:10:15,960 --> 00:10:19,350 ‫So now, of course, I had to adjust my introduce myself thing as well. 161 00:10:19,350 --> 00:10:25,350 ‫But here I want to make Frank 25 years old and I want Frank to introduce himself. 162 00:10:25,410 --> 00:10:27,480 ‫So now you see it's always the same. 163 00:10:27,690 --> 00:10:34,710 ‫We just need to adjust based on the parameters that we have available and then we just go ahead and 164 00:10:34,710 --> 00:10:35,610 ‫run the code. 165 00:10:35,610 --> 00:10:36,430 ‫So we are. 166 00:10:36,450 --> 00:10:37,540 ‫Hi, I'm Natalie. 167 00:10:37,560 --> 00:10:38,910 ‫Hi, I'm Michael Miller. 168 00:10:38,910 --> 00:10:43,710 ‫I am Frank Walter and 25 years old and so forth. 169 00:10:44,340 --> 00:10:44,730 ‫All right. 170 00:10:44,730 --> 00:10:52,050 ‫So now you have seen how to create multiple different constructors and you can see that we have a default 171 00:10:52,050 --> 00:10:54,930 ‫constructor which doesn't take any parameters. 172 00:10:54,930 --> 00:11:00,780 ‫And then we have different ones which have multiple different versions of amounts of parameters. 173 00:11:01,290 --> 00:11:01,860 ‫Great. 174 00:11:01,860 --> 00:11:09,570 ‫So now that you know that we should have a look at properties and they are super, super helpful and 175 00:11:09,570 --> 00:11:16,350 ‫useful and we will see how we can change values without having to make the variable public. 176 00:11:16,350 --> 00:11:23,310 ‫Because you have to be very careful with public variables of classes that you create because it can 177 00:11:23,310 --> 00:11:25,350 ‫be pretty tricky if you change those. 178 00:11:25,350 --> 00:11:30,960 ‫There can be a lot of security issues and if you not want to know more about that, I have a link that 179 00:11:30,960 --> 00:11:32,220 ‫I've shared with you. 180 00:11:32,220 --> 00:11:37,890 ‫You can just check that out and read in more detail what the purpose of using private here is and why 181 00:11:37,890 --> 00:11:38,670 ‫it's important. 182 00:11:38,700 --> 00:11:44,610 ‫So I'd say, see you in the next video where we are going to cover properties and yeah, have fun.