1 00:00:00,570 --> 00:00:01,470 ‫Welcome back. 2 00:00:01,500 --> 00:00:07,260 ‫In this video, we are going to use what we have seen in the last video in theory, but we're going 3 00:00:07,260 --> 00:00:08,160 ‫to do a demo here. 4 00:00:08,160 --> 00:00:16,470 ‫So we are going to create our own class, which is a blueprint of a data type, so we can create our 5 00:00:16,470 --> 00:00:19,500 ‫own data types by creating a class. 6 00:00:19,500 --> 00:00:23,850 ‫And that class can have properties, that class can have methods and so forth. 7 00:00:23,850 --> 00:00:26,550 ‫So let's go ahead and create a new class. 8 00:00:26,550 --> 00:00:34,410 ‫So I'm going to right click onto my C shop level and I'm going to create a new class here and I'm going 9 00:00:34,410 --> 00:00:38,520 ‫to call that one human dot CSS. 10 00:00:39,000 --> 00:00:40,110 ‫So now let's go ahead. 11 00:00:40,230 --> 00:00:45,450 ‫In my human class, I want to create members. 12 00:00:45,450 --> 00:00:51,720 ‫And members are, for example, variables, members can be methods and so forth, and they are called 13 00:00:51,720 --> 00:00:55,410 ‫members because they are part of that specific class. 14 00:00:55,410 --> 00:01:03,990 ‫So let's say I have a member variable and for a human that would be, for example, a string called 15 00:01:03,990 --> 00:01:08,790 ‫first name that would be a great member variable. 16 00:01:08,970 --> 00:01:11,160 ‫And so this human. 17 00:01:11,910 --> 00:01:14,760 ‫Is our data type so I can create. 18 00:01:15,610 --> 00:01:16,990 ‫An object off of that. 19 00:01:16,990 --> 00:01:20,860 ‫So I'm just going to create a human now I'm going to call the human. 20 00:01:20,860 --> 00:01:24,430 ‫Dennis is a new human and that's how I do it. 21 00:01:24,430 --> 00:01:32,680 ‫So this is how I create an object of my class. 22 00:01:34,140 --> 00:01:38,580 ‫Or also called an instance of human. 23 00:01:38,580 --> 00:01:44,670 ‫So this instance is called Dennis, and now this Dennis has a first name. 24 00:01:44,670 --> 00:01:48,780 ‫So if I would like to change the first name, there are multiple ways to implement that. 25 00:01:48,780 --> 00:01:52,020 ‫You could do that with a constructor and so forth, and we're going to do that. 26 00:01:52,020 --> 00:01:52,530 ‫No worries. 27 00:01:52,530 --> 00:01:53,610 ‫We're going to do all of that. 28 00:01:53,610 --> 00:01:55,440 ‫But I'm going to keep it simple in this video. 29 00:01:55,440 --> 00:01:59,130 ‫So you first of all understand that this is actually a blueprint. 30 00:01:59,130 --> 00:02:01,410 ‫So Class Human is a blueprint now. 31 00:02:01,410 --> 00:02:04,170 ‫And let's say I want to introduce myself. 32 00:02:04,170 --> 00:02:08,700 ‫So this human class has multiple capabilities or skills. 33 00:02:08,700 --> 00:02:10,470 ‫So, for example, you can drink. 34 00:02:10,470 --> 00:02:13,440 ‫All right, how much can you drink or how much do you drink? 35 00:02:13,440 --> 00:02:16,380 ‫So each time you drink, you could have a parameter. 36 00:02:16,380 --> 00:02:17,610 ‫How much did you drink? 37 00:02:17,610 --> 00:02:23,820 ‫So in order to see how hydrated you are, so let's say there is a hydration level from 0 to 100. 38 00:02:23,820 --> 00:02:25,260 ‫How hydrated are you? 39 00:02:25,260 --> 00:02:28,230 ‫And each time you drink, let's see, let's say half a liter. 40 00:02:28,260 --> 00:02:33,330 ‫Then it increases your hydration level by 30% or by 40%. 41 00:02:33,330 --> 00:02:39,120 ‫So that's how you could, for example, design a human in programming purposes. 42 00:02:39,120 --> 00:02:44,040 ‫So what I'm going to do is I'm just going to create a method which will introduce me. 43 00:02:44,040 --> 00:02:52,620 ‫And this is going to be a member method because it's part of my human class, which is a blueprint. 44 00:02:52,620 --> 00:03:03,870 ‫So this class is a blueprint for a data type, and this method is going to be a void, public void, 45 00:03:04,410 --> 00:03:06,930 ‫introduce myself. 46 00:03:06,930 --> 00:03:11,280 ‫And what it will do is it will simply write something onto the console. 47 00:03:11,280 --> 00:03:15,210 ‫It's going to write something like, Hi, I'm. 48 00:03:15,690 --> 00:03:18,330 ‫And then it's going to write the first name. 49 00:03:18,900 --> 00:03:19,440 ‫All right. 50 00:03:19,770 --> 00:03:22,860 ‫So now, as you can see, first name doesn't have a value. 51 00:03:22,860 --> 00:03:30,450 ‫And if I try to store something into Dennis, so if I try to change the first name, I can't do that. 52 00:03:30,450 --> 00:03:35,880 ‫That's because it's protected and it's protected because there is no public before that. 53 00:03:35,880 --> 00:03:43,830 ‫So if you don't add a public, this variable is going to be protected and not accessible from outside. 54 00:03:43,830 --> 00:03:49,140 ‫So from outside this class, I can still use it within this class, as you could see, because I have 55 00:03:49,140 --> 00:03:51,000 ‫used it here and it was no problem. 56 00:03:51,000 --> 00:03:59,280 ‫But if I make it public, I can now also use it from my program CSS so I can simply say Dennis first 57 00:03:59,280 --> 00:04:01,320 ‫name and I'm going to set that. 58 00:04:01,320 --> 00:04:03,870 ‫So I'm going to set that to Dennis. 59 00:04:03,900 --> 00:04:05,640 ‫Now I'm going to introduce Dennis. 60 00:04:05,640 --> 00:04:10,260 ‫So Dennis is going to introduce himself just like that. 61 00:04:10,260 --> 00:04:21,750 ‫So you see, this is how I can access private or actually is a public public variable from outside. 62 00:04:24,480 --> 00:04:26,100 ‫And even change it. 63 00:04:26,100 --> 00:04:35,040 ‫And this is how I can call methods of the class or in this case, of the object. 64 00:04:35,070 --> 00:04:40,740 ‫Dennis So Dennis is of type human, which means it's of this class type. 65 00:04:40,740 --> 00:04:44,850 ‫So this is the blueprint and it has a method called introduce myself. 66 00:04:44,850 --> 00:04:47,190 ‫And this is the method that I'm going to call here. 67 00:04:47,190 --> 00:04:56,910 ‫So if I run the code and in order to see something on the console, I also want to read the key so my 68 00:04:56,910 --> 00:04:59,430 ‫console doesn't shut down straight away. 69 00:05:00,520 --> 00:05:05,520 ‫And that we are so high and Dennis is written onto the console. 70 00:05:05,530 --> 00:05:10,930 ‫So let's say I create a new object which I'm going to call human. 71 00:05:10,960 --> 00:05:14,590 ‫Michael is a new human. 72 00:05:15,730 --> 00:05:21,460 ‫And now if I introduce Michael, so introduce myself. 73 00:05:21,580 --> 00:05:27,490 ‫If I just run the code like that, you will see that it just says high empty space. 74 00:05:27,610 --> 00:05:30,640 ‫And that's because we didn't set the name of Michael. 75 00:05:31,450 --> 00:05:36,790 ‫So let's say we want to save the name or change the name straight, straight away in here. 76 00:05:36,790 --> 00:05:40,630 ‫So I'm just going to say, Michael, here, let's test that. 77 00:05:41,230 --> 00:05:42,340 ‫Let's run the code. 78 00:05:44,160 --> 00:05:46,140 ‫And in this case, it's actually going to work. 79 00:05:46,140 --> 00:05:48,510 ‫So it's going to say, Hi, I'm Dennis and hi, I'm Michael. 80 00:05:48,510 --> 00:05:55,560 ‫So what we do here is we say every single human is going to be called Michael as long as you don't change 81 00:05:55,560 --> 00:05:55,910 ‫it. 82 00:05:55,920 --> 00:06:01,950 ‫So Dennis has actually actually changed it, but Michael has never changed the name. 83 00:06:01,950 --> 00:06:03,570 ‫So Michael is still going to be Michael. 84 00:06:03,570 --> 00:06:10,290 ‫But if we, let's say, change the name Michael to Michael or something like that. 85 00:06:10,290 --> 00:06:17,400 ‫So let's say he wants to introduce himself in a different language and there he's going to be called 86 00:06:18,600 --> 00:06:25,170 ‫Michael or for example, Michael would be an option then. 87 00:06:25,960 --> 00:06:27,040 ‫It would be overridden. 88 00:06:27,040 --> 00:06:27,610 ‫And we see. 89 00:06:27,640 --> 00:06:28,360 ‫Hi, I'm Dennis. 90 00:06:28,360 --> 00:06:29,290 ‫And hi, I'm Michael. 91 00:06:30,460 --> 00:06:31,150 ‫All right. 92 00:06:31,510 --> 00:06:37,150 ‫So this is how you can actually make sure that this always the same. 93 00:06:37,150 --> 00:06:41,070 ‫So this variable is going to be always the same first name, except you change it. 94 00:06:41,080 --> 00:06:45,070 ‫So now if I make it private, so let me stop that. 95 00:06:45,070 --> 00:06:51,910 ‫I'm going to make it private string Michael Now I'm going to receive an error here because I cannot 96 00:06:51,910 --> 00:06:54,790 ‫change the first name because it's private now. 97 00:06:54,790 --> 00:06:59,890 ‫So neither Denis nor Mike, Michael or Michael can now change their name. 98 00:06:59,890 --> 00:07:02,230 ‫So those two objects are going to be. 99 00:07:03,110 --> 00:07:03,860 ‫The same. 100 00:07:03,860 --> 00:07:04,670 ‫So it's going to be. 101 00:07:04,670 --> 00:07:05,600 ‫Hi, I'm Michael. 102 00:07:05,630 --> 00:07:06,470 ‫I am Michael. 103 00:07:06,500 --> 00:07:08,180 ‫So they don't differ in any way. 104 00:07:08,180 --> 00:07:11,930 ‫So now you see why it makes sense to make this method here public. 105 00:07:11,930 --> 00:07:17,900 ‫And it's actually a public method, and it really doesn't seem to make sense to make this private, 106 00:07:17,900 --> 00:07:19,540 ‫because now I cannot change it. 107 00:07:19,550 --> 00:07:22,810 ‫Well, actually, there are, as I said, multiple ways to change it. 108 00:07:22,820 --> 00:07:25,070 ‫One of them is creating a property. 109 00:07:25,070 --> 00:07:29,990 ‫The other one is to create constructors, and we're going to do both of that. 110 00:07:29,990 --> 00:07:37,080 ‫So I'm going to start off with constructors in the next video where you will see how to go ahead with 111 00:07:37,080 --> 00:07:37,490 ‫that. 112 00:07:37,490 --> 00:07:40,820 ‫So now, first of all, a little challenge for you. 113 00:07:40,820 --> 00:07:46,490 ‫So create a public variable last name of type string change, introduce myself method. 114 00:07:46,490 --> 00:07:47,870 ‫So it tells the whole name. 115 00:07:47,870 --> 00:07:51,110 ‫So it doesn't just say the first name, but also the last name. 116 00:07:51,110 --> 00:07:57,880 ‫Then create two objects with full information about themselves in our program CSS. 117 00:07:57,920 --> 00:07:59,090 ‫So in the main method. 118 00:08:01,520 --> 00:08:01,880 ‫All right. 119 00:08:01,880 --> 00:08:03,830 ‫I hope you pause the video and try to do that. 120 00:08:03,830 --> 00:08:14,930 ‫So let's go ahead and I'm going to make it public starring last name, which is going to be empty for 121 00:08:14,930 --> 00:08:15,490 ‫now. 122 00:08:15,530 --> 00:08:16,670 ‫The same here. 123 00:08:16,700 --> 00:08:23,420 ‫I'm going to change that first name to not being Michael anymore because not everyone is called Michael. 124 00:08:24,140 --> 00:08:25,460 ‫Then I'm changing. 125 00:08:25,460 --> 00:08:29,480 ‫Introduce myself to also tell the last name. 126 00:08:30,110 --> 00:08:33,380 ‫So I'm going to need that here last name. 127 00:08:34,520 --> 00:08:37,910 ‫And now let's create two objects with full information about themselves. 128 00:08:37,910 --> 00:08:39,410 ‫So I'm going to get rid of. 129 00:08:39,410 --> 00:08:41,870 ‫So actually let me just clean up everything here. 130 00:08:42,380 --> 00:08:54,170 ‫So first of all, human and I'm going to call her Sissy is a new human and now Sissy has a first name. 131 00:08:54,170 --> 00:09:00,950 ‫So first name is going to be Sissy and her last name. 132 00:09:00,950 --> 00:09:04,340 ‫Well, actually, I don't know what Sissy last name is. 133 00:09:04,340 --> 00:09:05,930 ‫I'm going to call a Wagner. 134 00:09:07,280 --> 00:09:19,460 ‫And then we have Sissy who introduces herself, introduce myself, and now we have a different person, 135 00:09:19,910 --> 00:09:24,200 ‫human frontal, new human. 136 00:09:25,370 --> 00:09:32,440 ‫And that frontal is going to be called actually frontal. 137 00:09:33,170 --> 00:09:38,750 ‫It's a very uncommon name, but it's a very old funny name, an Austrian. 138 00:09:38,750 --> 00:09:45,830 ‫And that frontal last name is going to be Mila. 139 00:09:46,940 --> 00:09:48,860 ‫You can actually write it with you as well. 140 00:09:48,860 --> 00:09:53,810 ‫You don't need to use this this special sign here. 141 00:09:54,380 --> 00:09:54,710 ‫Okay. 142 00:09:54,830 --> 00:09:57,860 ‫And that person should introduce himself as well. 143 00:09:57,860 --> 00:10:02,210 ‫So introduce myself is what he's going to say. 144 00:10:02,210 --> 00:10:05,750 ‫So let's run the code and we see. 145 00:10:05,750 --> 00:10:07,160 ‫Hi, I'm Sissy Wagner. 146 00:10:07,160 --> 00:10:08,650 ‫Hi, I'm Franz Von Miller. 147 00:10:09,080 --> 00:10:09,980 ‫So there we are. 148 00:10:10,010 --> 00:10:15,680 ‫You see how to use human or how to use your own data type. 149 00:10:15,680 --> 00:10:20,900 ‫So we're going to look into that a bit more because for now, as I said, this is just a member, just 150 00:10:20,900 --> 00:10:21,620 ‫a variable. 151 00:10:21,620 --> 00:10:24,020 ‫And I talked about properties earlier on. 152 00:10:24,020 --> 00:10:29,720 ‫So for example, length was a property, but properties are written with a capital letter at the start 153 00:10:29,720 --> 00:10:35,150 ‫and we're going to look into properties and of course we're also going to look into constructors because 154 00:10:35,150 --> 00:10:40,220 ‫why would I have to do it this way if I can simply do the whole thing in one line? 155 00:10:40,220 --> 00:10:47,660 ‫So let's say I could enter something like Sissy and Wagner right here, and that human would already 156 00:10:47,660 --> 00:10:48,380 ‫be created. 157 00:10:48,380 --> 00:10:52,730 ‫Then I wouldn't have to do all of these additional lines, so I could simply do that in the same line. 158 00:10:52,730 --> 00:10:57,560 ‫And that's exactly what you can achieve by using constructors, and that's what we're going to do in 159 00:10:57,560 --> 00:10:58,430 ‫the next video. 160 00:10:58,430 --> 00:11:02,690 ‫So I hope you could manage to do the little challenge that you have here. 161 00:11:02,690 --> 00:11:03,740 ‫In the next video. 162 00:11:03,740 --> 00:11:07,460 ‫We're going to have a look at constructors first before we go into properties. 163 00:11:07,460 --> 00:11:09,140 ‫So see you in the next video.