1 00:00:00,630 --> 00:00:01,440 Welcome back. 2 00:00:01,620 --> 00:00:07,170 And this video, we are going to look into classes, so let's go ahead and create our own class, and 3 00:00:07,170 --> 00:00:10,620 therefore we can have a quick look at classes that we have seen already. 4 00:00:10,860 --> 00:00:17,880 And we've seen this class called main activity, which has this colon app complete activity here, which 5 00:00:17,880 --> 00:00:23,170 just means that this is going to be the class that our classes inheriting from. 6 00:00:23,190 --> 00:00:26,820 So this is a concept called inheritance, which we will look at later on. 7 00:00:27,330 --> 00:00:31,830 But basically, what we see is that we have this class key word and then we have a class name, which 8 00:00:31,830 --> 00:00:33,060 starts with a capital letter. 9 00:00:33,570 --> 00:00:37,740 So let's go ahead and create our own class, and I'm going to call this class person. 10 00:00:38,340 --> 00:00:40,890 So this by itself is valid code already. 11 00:00:40,890 --> 00:00:42,360 So we have this class person. 12 00:00:43,140 --> 00:00:45,560 And of course, this class doesn't have any properties. 13 00:00:45,570 --> 00:00:48,330 It doesn't have any methods, none of that. 14 00:00:48,600 --> 00:00:50,910 But this is a world class. 15 00:00:51,420 --> 00:00:52,950 Now, of course, we can extend that. 16 00:00:53,040 --> 00:00:55,950 We can go ahead and say that it should have a constructor. 17 00:00:56,520 --> 00:01:06,660 So a constructor allows us to add values to our objects when we create an object of our class, and 18 00:01:07,080 --> 00:01:09,930 these values could be something like the name. 19 00:01:10,800 --> 00:01:15,330 And it's just not worth the first name here and last name. 20 00:01:15,450 --> 00:01:22,590 So we could just go ahead and have these two variables here as the very built of the constructor. 21 00:01:22,830 --> 00:01:25,710 So these are the parameters of the constructor, so to say. 22 00:01:26,130 --> 00:01:32,280 And then in curly brackets, that's where the class body starts or where it is. 23 00:01:32,370 --> 00:01:34,680 So here it starts, and that's where it ends. 24 00:01:34,980 --> 00:01:41,870 So in here we have our class body and we can do all of the defining of our blueprint. 25 00:01:41,880 --> 00:01:47,550 So here we can go ahead and create all of the different properties and methods and those kind of things. 26 00:01:48,990 --> 00:01:53,320 So now this constructive year, as you've seen here, it wasn't even there. 27 00:01:53,340 --> 00:01:54,690 So there's no constructor there. 28 00:01:55,050 --> 00:02:01,800 And that is because it is not mandatory if we don't want to define the constructor specifically. 29 00:02:01,800 --> 00:02:06,990 So if we don't want to go ahead and make this one private, for example, or if we don't want to make 30 00:02:06,990 --> 00:02:13,110 this public or whatever, so we can just get rid of the constructor and make this code even easier to 31 00:02:13,110 --> 00:02:13,350 read. 32 00:02:13,350 --> 00:02:18,900 So we have class person and then and then we have these two values that need to be passed once we create 33 00:02:19,170 --> 00:02:21,330 an object of our class person. 34 00:02:21,780 --> 00:02:27,900 So how do we create an object of a class or how do we create an instance of a class? 35 00:02:27,900 --> 00:02:29,460 That's another way of saying it. 36 00:02:30,030 --> 00:02:33,690 Therefore, we just go ahead and use the name of our class. 37 00:02:34,020 --> 00:02:41,010 So in our case person and then in brackets, that's where we want to go ahead and. 38 00:02:42,330 --> 00:02:48,780 Adds the two values here, so I can see first name and last name, so I could do something like Dennis 39 00:02:49,800 --> 00:02:50,160 one you. 40 00:02:50,520 --> 00:02:51,360 All right, that's me. 41 00:02:51,690 --> 00:02:58,710 So I have this object of class person, which contains those two values, which are first name and last 42 00:02:58,710 --> 00:02:58,980 name. 43 00:02:59,580 --> 00:03:07,020 So if I now go ahead and create a variable called Dennis, it's going to be a variable of a type called 44 00:03:07,020 --> 00:03:07,470 person. 45 00:03:07,650 --> 00:03:10,980 So I could, of course, state that specifically here. 46 00:03:11,010 --> 00:03:17,880 I could say Dennis is going to be of type person, but as you know, Carlin does the job for us because 47 00:03:17,880 --> 00:03:23,700 it understands, OK, if I create an object here after the equal sign of a specific class, then it 48 00:03:23,700 --> 00:03:24,810 will be of that class. 49 00:03:25,200 --> 00:03:27,480 So of that type, so to speak. 50 00:03:27,720 --> 00:03:31,740 So by creating a class, we created our own type. 51 00:03:32,700 --> 00:03:33,090 All right. 52 00:03:33,570 --> 00:03:36,180 Such as we have seen in the past already. 53 00:03:36,180 --> 00:03:39,630 So we used a bunch of different types and now we are using our own type. 54 00:03:39,960 --> 00:03:44,220 So instead of using incense and strings and those kind of things, we use our own type, which is called 55 00:03:44,220 --> 00:03:46,260 person whenever we create a class. 56 00:03:46,500 --> 00:03:49,610 We can also have an in it there. 57 00:03:49,620 --> 00:03:54,480 So this is to initialize them, which is called once an object is created. 58 00:03:54,840 --> 00:04:00,130 So as soon as we run this code, it will automatically run this initialize. 59 00:04:00,720 --> 00:04:08,040 So we can just go ahead and write something like person created and I will run this code. 60 00:04:09,870 --> 00:04:13,950 We will see that person created is printed to our console. 61 00:04:14,670 --> 00:04:20,640 All right, so this is the initialism, and you can use it in order to initialize your object. 62 00:04:22,560 --> 00:04:25,080 Which pretty much means prepare your object. 63 00:04:26,190 --> 00:04:31,380 Another way of saying person created would be initialized a new person object. 64 00:04:31,500 --> 00:04:35,460 So that's another way what we could or another thing that we could state here. 65 00:04:35,850 --> 00:04:39,630 So what we can also say is what person we created. 66 00:04:39,630 --> 00:04:45,030 So, for example, that we create a person with a specific first and last name. 67 00:04:45,810 --> 00:04:48,020 So I'm just going to paste the code in here. 68 00:04:48,030 --> 00:04:53,040 So we say here initialize the new person with first name. 69 00:04:53,500 --> 00:04:56,810 Then we use the first name of our person and last name with them. 70 00:04:56,820 --> 00:04:59,130 We used to last name of our person. 71 00:04:59,640 --> 00:05:08,220 So you can see it's like us creating variables here when we use them in our constructor up there. 72 00:05:09,580 --> 00:05:09,970 All right. 73 00:05:10,150 --> 00:05:14,770 So if you run that now, we should see that the person that we created is Dennis Potuto. 74 00:05:16,260 --> 00:05:21,360 So let me run it again, initialize the new person object with first name Dennis and last name upon 75 00:05:21,370 --> 00:05:21,720 you, Tom. 76 00:05:22,080 --> 00:05:27,630 If you want to define default values for your first and last name, for example, you can do so as well. 77 00:05:27,660 --> 00:05:33,960 So, for example, I'm going to use John Doe here as default values. 78 00:05:34,290 --> 00:05:35,400 So John Doe. 79 00:05:35,820 --> 00:05:41,560 And now if I go ahead and create a new object and I'm just going to call this one, John. 80 00:05:43,440 --> 00:05:50,640 Then this will automatically be John Doe, this person, even though we didn't specifically say that 81 00:05:50,850 --> 00:05:54,300 this person should be called John Doe when creating this object. 82 00:05:54,930 --> 00:06:03,710 OK, so when we're getting the instance of this class person and here this is because we defined those 83 00:06:03,720 --> 00:06:04,560 default values. 84 00:06:05,400 --> 00:06:07,050 So if we run this, we can see. 85 00:06:07,990 --> 00:06:11,200 Initialize the new person object, my first name, John, last name, though. 86 00:06:12,320 --> 00:06:15,800 Let's say we only have the last name. 87 00:06:16,400 --> 00:06:18,950 So I'm just going to call this person John Peterson. 88 00:06:20,600 --> 00:06:22,250 It's going to be a new person. 89 00:06:23,330 --> 00:06:28,610 And here I just say that I want to define the last name as being Peterson. 90 00:06:29,600 --> 00:06:30,710 So if we run this now? 91 00:06:32,220 --> 00:06:40,590 Then we will have three people that were created and two or three instances of our class that were created, 92 00:06:41,010 --> 00:06:46,890 and the last one will have the last name Peterson and the first name John so that we are initialized 93 00:06:46,890 --> 00:06:51,000 a new person object with a first name, John and last name Peterson. 94 00:06:51,960 --> 00:06:52,320 All right. 95 00:06:52,320 --> 00:06:55,230 So this is just one example of how to create classes. 96 00:06:55,240 --> 00:07:01,860 There are multiple different ones that you can use, and in the next video, we are going to see how 97 00:07:01,860 --> 00:07:07,290 properties work and also how methods work so how we can create functions within classes. 98 00:07:08,130 --> 00:07:08,850 So we'll see you there.