1 00:00:00,680 --> 00:00:06,260 Next, we're going to look at members of classes, and there are multiple different types of members. 2 00:00:06,590 --> 00:00:15,080 The simplest one are member variables, so no variable, let's call them properties. 3 00:00:17,030 --> 00:00:23,540 Alternatively, so you can either call them a verbal or properties, what would be an example property 4 00:00:23,540 --> 00:00:26,300 for a person or a useful property? 5 00:00:27,440 --> 00:00:31,610 Well, there are plenty, but for example, age would be a very useful one. 6 00:00:32,030 --> 00:00:34,940 So let's say we have the age. 7 00:00:36,800 --> 00:00:39,430 And it can be, no, okay. 8 00:00:40,100 --> 00:00:44,600 So why would I create a variable called age that can be null for a person? 9 00:00:45,260 --> 00:00:52,400 Well, the thing is that let's say we have the data of a bunch of people, so a bunch of users in our 10 00:00:52,400 --> 00:00:53,000 database. 11 00:00:53,450 --> 00:00:56,660 And for most of them, we don't have the age. 12 00:00:56,960 --> 00:01:00,680 For some of them, we do have the age, but we have all of them the first name and the last name. 13 00:01:02,030 --> 00:01:07,370 So we want our program to run no matter if we have the age or not, it should still work. 14 00:01:07,730 --> 00:01:11,180 But if we have the age, then we maybe want to do something special. 15 00:01:11,450 --> 00:01:19,250 We either want to display it on the app or on the website, or we could even go so far and to say Happy 16 00:01:19,250 --> 00:01:20,270 Birthday or whatever. 17 00:01:20,510 --> 00:01:24,230 If we don't have just the age, but the actual birth date, for example. 18 00:01:25,070 --> 00:01:25,460 All right. 19 00:01:25,820 --> 00:01:29,810 So this is an example of a member variable, and we can have more of those. 20 00:01:29,820 --> 00:01:37,040 So let's say I have a hobby here as well, which I'm going to define as and watch Netflix. 21 00:01:38,900 --> 00:01:40,700 So this is going to be the default value. 22 00:01:41,920 --> 00:01:44,590 And this is an example for the member variables. 23 00:01:45,220 --> 00:01:47,680 Now let's have a look at member functions. 24 00:01:47,920 --> 00:01:51,040 So there is something called member functions. 25 00:01:51,760 --> 00:01:53,380 You can also call them methods. 26 00:01:53,890 --> 00:01:54,340 All right. 27 00:01:55,540 --> 00:02:02,740 And a member function is a function within a class as a member, variable is a variable within a class. 28 00:02:03,080 --> 00:02:11,710 OK, so the member function that I'm going to create is just going to be a very simple one, which is 29 00:02:11,710 --> 00:02:14,140 going to state the hobby of someone. 30 00:02:15,190 --> 00:02:15,670 OK? 31 00:02:16,030 --> 00:02:19,120 So the only thing that it should say is front line. 32 00:02:20,980 --> 00:02:23,380 My hobby is. 33 00:02:23,890 --> 00:02:25,390 And then the hobby. 34 00:02:27,250 --> 00:02:27,640 All right. 35 00:02:28,090 --> 00:02:33,070 So another cool thing is we can go ahead and do the following. 36 00:02:33,340 --> 00:02:35,620 So let's say then is that a state hobby? 37 00:02:36,460 --> 00:02:39,730 So let's see what will be printed onto our console. 38 00:02:40,940 --> 00:02:45,800 So there we are, initialized in new person object first names and his last name by Newton, my hobby 39 00:02:45,800 --> 00:02:48,320 is Netflix or is watch Netflix. 40 00:02:49,130 --> 00:02:51,230 OK, so that is cool. 41 00:02:51,380 --> 00:02:54,300 But now the problem is the hobby is the same for everyone. 42 00:02:54,380 --> 00:02:55,580 So how can I change it? 43 00:02:56,240 --> 00:02:57,950 Well, you can change it the following way. 44 00:02:57,960 --> 00:03:05,720 You can go ahead and say Dennis thought hobby should be something different, such as skateboard. 45 00:03:06,560 --> 00:03:08,960 All right, so maybe Dennis likes to skateboard. 46 00:03:09,860 --> 00:03:14,900 And then if you run this code again, we will see that now my hobby is skateboarding. 47 00:03:16,360 --> 00:03:22,240 Maybe we should add something like to skateboard in here or something like that, but yeah, overall 48 00:03:22,480 --> 00:03:23,490 the logic is the same. 49 00:03:24,050 --> 00:03:27,460 Okay, so what we now can do is and that is pretty cool. 50 00:03:27,490 --> 00:03:34,360 We can have an individual hobby for every single person because every single person already has something 51 00:03:34,360 --> 00:03:37,180 that is unique to them or individual to them. 52 00:03:37,510 --> 00:03:43,870 Unique is a little difficult to say because probably there are other John Doe's in the world. 53 00:03:44,200 --> 00:03:51,460 But what we can say now is that the hobby of John should be something else, such as play video games. 54 00:03:53,230 --> 00:03:59,080 All right, so John Doe likes to play video games, and now we can state his hobbies here as well. 55 00:03:59,590 --> 00:04:06,280 So I'm just going to say John thought state hobbies. 56 00:04:07,410 --> 00:04:11,830 Now, if we run this, then we'll see that John's hobby is to play video games. 57 00:04:12,610 --> 00:04:18,610 So this is just an example for why object orientation is so powerful or object oriented programming, 58 00:04:18,910 --> 00:04:28,030 because we can now create blueprints, our classes for our own data types or objects that we can then 59 00:04:28,030 --> 00:04:28,900 go ahead and use. 60 00:04:28,900 --> 00:04:31,840 So I create an object here, which is a variable. 61 00:04:31,880 --> 00:04:38,770 So a variable is an object if it is of a type, which is a class. 62 00:04:39,050 --> 00:04:42,910 OK, so in Scotland, pretty much everything is an object. 63 00:04:44,360 --> 00:04:50,930 Even the basic and generic data types, such as ins and strengths are classes sold to say. 64 00:04:52,070 --> 00:04:58,160 And you can see that when you entered into keywords, you can see that this is a sea, which means that 65 00:04:58,160 --> 00:04:59,910 this is a class game. 66 00:05:01,490 --> 00:05:03,680 So we create our own data types here. 67 00:05:04,280 --> 00:05:11,420 We can change their properties and we can use their methods, which we can define ourselves as well, 68 00:05:11,420 --> 00:05:14,000 so we can go ahead and create as many methods as we want. 69 00:05:15,020 --> 00:05:16,730 In the last video, we looked at scope. 70 00:05:17,090 --> 00:05:18,530 Why would we do that? 71 00:05:18,780 --> 00:05:22,280 Because we're not using scope here, and I said that we needed scope in here. 72 00:05:22,820 --> 00:05:25,910 Well, because we are using first name here, right? 73 00:05:26,150 --> 00:05:32,720 So in our in it, in our initial laser block, we use this variable name, first name and where does 74 00:05:32,720 --> 00:05:36,590 it come from, where it comes from, our constructor here. 75 00:05:36,680 --> 00:05:38,780 So this is our primary constructor. 76 00:05:39,830 --> 00:05:44,310 And now in order to understand that, we need to create another constructor. 77 00:05:44,330 --> 00:05:52,010 So what I'm going to do is I'm going to create a constructor here, and a constructor is also a member, 78 00:05:52,430 --> 00:05:53,870 member constructor. 79 00:05:54,950 --> 00:05:55,920 To be more precise. 80 00:05:55,920 --> 00:05:58,700 This is a secondary constructor. 81 00:05:59,420 --> 00:05:59,780 All right. 82 00:06:00,200 --> 00:06:06,770 So what I can do is I can now say what kind of variables I would like to have when somebody is creating 83 00:06:06,770 --> 00:06:10,040 an object of type person who did that already here. 84 00:06:10,220 --> 00:06:15,470 But I can have a secondary constructor where I say I want also to have the H. 85 00:06:16,040 --> 00:06:21,920 So whenever somebody creates an object of person, they can also pass in the H and we will then use 86 00:06:21,920 --> 00:06:22,070 it. 87 00:06:22,760 --> 00:06:26,330 All right, so I can go ahead and say, OK, give me the first name. 88 00:06:26,840 --> 00:06:28,100 Give me the last name. 89 00:06:28,460 --> 00:06:32,870 But also give me the edge, which will be of Typekit. 90 00:06:34,750 --> 00:06:43,060 Now, what we need to do here is we also need to add a column and then this first name, last name because 91 00:06:43,060 --> 00:06:44,140 we are using the two. 92 00:06:45,130 --> 00:06:47,530 OK, so I'm just going to put that in the second line here. 93 00:06:48,460 --> 00:06:55,990 So this is our new constructor that we created our secondary constructor and now we can go ahead and 94 00:06:56,920 --> 00:07:02,830 say that our h that we have, there should be the h that is passed to us. 95 00:07:03,730 --> 00:07:05,980 So I'm using this, this keyboard here twice. 96 00:07:06,070 --> 00:07:07,970 So you might wonder what is does this? 97 00:07:08,410 --> 00:07:11,590 Well, this this is a little easier to understand. 98 00:07:11,620 --> 00:07:19,000 So what I'm saying is that the H that is given to the constructor when the object is created should 99 00:07:19,000 --> 00:07:27,100 be assigned to the edge of this class, which means that it's going to be this one here. 100 00:07:27,730 --> 00:07:29,620 This H that we created here at the top. 101 00:07:29,860 --> 00:07:34,960 So to the member variable or to the property, it should be assigned to the property. 102 00:07:35,260 --> 00:07:36,610 So that's what I'm saying with this. 103 00:07:37,510 --> 00:07:47,410 OK, now this this here, it says that the first name and last name that we're using here in our secondary 104 00:07:47,410 --> 00:07:52,450 constructor should be the ones coming from our primary constructor. 105 00:07:52,600 --> 00:07:56,920 So there should be the first name that is passed here, John. 106 00:07:57,190 --> 00:08:01,270 And the second name that is passed here, which is the last name. 107 00:08:02,020 --> 00:08:06,910 So what we can do now is we can go ahead and give some more information. 108 00:08:07,420 --> 00:08:13,810 So let's say I create a person which is called Dennis, and I also pass to H. 109 00:08:14,020 --> 00:08:18,490 So I don't only pass the first name and last name, but I also pass in the H. 110 00:08:19,120 --> 00:08:21,340 So Dennis will now have an h. 111 00:08:21,760 --> 00:08:31,930 So if I go ahead and say Dennis, that H is equal to Th2, then the initial h that was set will be overwritten. 112 00:08:32,830 --> 00:08:37,530 So now if I want to print it, I can just do that. 113 00:08:37,539 --> 00:08:40,299 Print Dennis Start H. 114 00:08:42,530 --> 00:08:45,200 And maybe I should add a little more information. 115 00:08:46,030 --> 00:08:51,110 Dennis is then is that age old? 116 00:08:52,460 --> 00:08:54,260 And here we need to use brackets. 117 00:08:56,600 --> 00:08:57,800 Years old. 118 00:08:59,890 --> 00:09:01,090 So now let's run this again. 119 00:09:02,220 --> 00:09:10,260 And we are so we can see initialized the new person last name, then Newtown, and then Dennis is 32 120 00:09:10,260 --> 00:09:10,980 years old. 121 00:09:11,400 --> 00:09:14,070 And yeah, as a hobby of skateboarding. 122 00:09:15,260 --> 00:09:22,670 All right, so now the cool thing is we can provide an own print and statement when this other initialize 123 00:09:22,670 --> 00:09:23,240 I is called. 124 00:09:23,990 --> 00:09:31,140 So here I'm just going to say initialize the new person object with first name, last name and then. 125 00:09:31,970 --> 00:09:33,140 And H. 126 00:09:33,890 --> 00:09:34,970 So let's run this again. 127 00:09:35,390 --> 00:09:37,880 So now add initialized the. 128 00:09:39,020 --> 00:09:39,860 First constructor. 129 00:09:39,980 --> 00:09:46,770 So our primary constructor here, which was run by this initial as a block, as we saw earlier. 130 00:09:47,180 --> 00:09:51,560 And then it render constructor, which is our secondary constructor. 131 00:09:52,680 --> 00:09:54,750 And added this and here. 132 00:09:57,280 --> 00:09:57,730 All right. 133 00:09:58,090 --> 00:10:04,690 So now another thing about the first name, so let's say I want to extend my state hobby here. 134 00:10:05,080 --> 00:10:07,180 I want to say whose hobby it is. 135 00:10:07,390 --> 00:10:14,260 So he just says my hobby, but we didn't or we don't know who my is because of course, here we know 136 00:10:14,260 --> 00:10:16,360 that it's John, but that's because we programmed it. 137 00:10:16,690 --> 00:10:23,200 But what if we only see the output so we will never know which person's hobby it is? 138 00:10:23,530 --> 00:10:23,920 All right. 139 00:10:24,460 --> 00:10:30,610 So in order to change that, I want to go ahead and to finally come back to where I started with this 140 00:10:30,610 --> 00:10:31,120 whole thing. 141 00:10:31,420 --> 00:10:32,920 I want to use the first name here. 142 00:10:34,460 --> 00:10:36,050 And I want to use it like this. 143 00:10:37,220 --> 00:10:40,280 The problem is, this doesn't work, why does it not work? 144 00:10:40,760 --> 00:10:46,010 Well, because the first name isn't the variable that is invisible because it's only visible within 145 00:10:46,010 --> 00:10:48,200 this primary constructor here. 146 00:10:48,770 --> 00:10:53,780 And it's also visible within our secondary constructor now that we have thunder. 147 00:10:54,380 --> 00:10:59,720 So what I did with the H is what I can now do with the first name as well. 148 00:11:00,200 --> 00:11:02,120 So please go ahead and try it. 149 00:11:02,570 --> 00:11:03,710 Create a new variable. 150 00:11:04,250 --> 00:11:10,940 You can call it a little different just to avoid confusion and then use it in your state hobby as well. 151 00:11:13,780 --> 00:11:14,140 All right. 152 00:11:14,320 --> 00:11:18,340 So I hope you tried it, and maybe the exercise wasn't 100 percent clear. 153 00:11:18,370 --> 00:11:19,210 I'm sorry for that. 154 00:11:19,510 --> 00:11:29,230 But the idea is that we're creating a new first name here, which should be of type string, and it 155 00:11:29,230 --> 00:11:31,420 couldn't be now, so it can be empty. 156 00:11:33,090 --> 00:11:41,650 And then we go ahead and in our in it, so in our initialize up, we assign to this first name, we 157 00:11:41,650 --> 00:11:42,480 assign the first name. 158 00:11:43,410 --> 00:11:44,130 So we do. 159 00:11:45,540 --> 00:11:54,000 What we did here, so we take this first name and we store it within the member variable, so now we 160 00:11:54,000 --> 00:11:56,400 can go ahead and use it in our state hobby. 161 00:11:57,090 --> 00:12:00,180 So here I can go ahead and first name. 162 00:12:01,660 --> 00:12:04,240 And I'm going to use a. 163 00:12:05,970 --> 00:12:06,750 Backslash. 164 00:12:07,730 --> 00:12:08,630 Apostrophe here. 165 00:12:09,260 --> 00:12:14,180 And s just to say, for example, Dennis's hobby is Holly. 166 00:12:15,180 --> 00:12:16,290 So let's run this again. 167 00:12:17,400 --> 00:12:23,610 And now we see Venice's hobby is to skateboard, Jones hobby is play video games. 168 00:12:24,660 --> 00:12:33,000 So now we have a lot more knowledge about which object we have manipulated and which object does what. 169 00:12:34,200 --> 00:12:34,650 All right. 170 00:12:34,650 --> 00:12:40,470 So I think this is enough for this video, enough of different concepts that we're using. 171 00:12:40,920 --> 00:12:46,560 I told you that the class can have multiple members and the ones that we saw are not the only ones. 172 00:12:48,320 --> 00:12:54,770 Classes can contain multiple members here, constructors and initialize of blocks, functions, properties 173 00:12:54,770 --> 00:12:58,310 nested in the classes and object declarations. 174 00:12:58,760 --> 00:13:01,310 And we're going to see what those different things are later on. 175 00:13:01,700 --> 00:13:06,800 But for now, just know that there are multiple members that a class can have.