1 00:00:00,390 --> 00:00:06,000 Hi this is sharings and hard and welcome to the next video of this object oriented programming in Cartland. 2 00:00:06,020 --> 00:00:13,210 Now in this video shake out how do we define a class in case of Codling similar to Java in case of Cartland. 3 00:00:13,350 --> 00:00:18,590 We have the class definition like this class keyword followed by the class name. 4 00:00:18,690 --> 00:00:21,480 And this is the class Bardy. 5 00:00:21,720 --> 00:00:26,680 And now this class name customer is actually known as the holder of the class. 6 00:00:26,970 --> 00:00:28,980 So always remember this. 7 00:00:28,980 --> 00:00:32,400 Now how do we define the constructor in case of Cartland. 8 00:00:32,400 --> 00:00:39,390 So here is some example class customer followed by Bechard open and close inside which we can simply 9 00:00:39,390 --> 00:00:44,070 define some parameters such as name string like this. 10 00:00:44,070 --> 00:00:49,350 So this name is actually the string and it is actually the primary constructor. 11 00:00:50,190 --> 00:00:54,480 So this is the way we can define the primary constructor in case of Codlin. 12 00:00:54,480 --> 00:01:01,990 Now if you notice here this constructor does not contain any Borghi unlike in Java where we had the 13 00:01:01,990 --> 00:01:05,640 constructor defined explicitly inside the class body. 14 00:01:06,080 --> 00:01:11,380 Now the question arises how can we initialize all the field variables in case of Cartland. 15 00:01:11,380 --> 00:01:13,900 So for this we have entered block. 16 00:01:14,070 --> 00:01:20,450 So in it followed by Curly Blackard open and close inside this we can simply initialize all the properties 17 00:01:20,770 --> 00:01:22,060 all the field variables. 18 00:01:22,090 --> 00:01:24,770 In case of Cartland. 19 00:01:24,860 --> 00:01:31,050 So this is a typical example of a class containing the primary constructor and also in a block. 20 00:01:31,150 --> 00:01:36,110 Let us not proceed and check out what are the concepts and are we really covered in this value. 21 00:01:36,120 --> 00:01:42,330 So first of all we really check out what are properties in case of Cartland that are also known as the 22 00:01:42,330 --> 00:01:44,800 Field Theory was in case of Java. 23 00:01:45,150 --> 00:01:51,930 And then given that check out the significance of the primary constructor in a block primary constructor 24 00:01:51,930 --> 00:01:56,850 with the property and also the primary constructor it just goes. 25 00:01:56,960 --> 00:02:02,390 Now what is the basic difference between these two such as them reconstructed with property and with 26 00:02:02,540 --> 00:02:03,380 it goes. 27 00:02:03,390 --> 00:02:05,320 We will check out this video. 28 00:02:05,640 --> 00:02:07,520 So let's go to Stockard. 29 00:02:07,680 --> 00:02:13,010 So here inside the ID here I have my main medtech now on the side. 30 00:02:13,040 --> 00:02:17,860 Let us create our class such as a student right. 31 00:02:17,950 --> 00:02:21,570 And this is a class Bardy. 32 00:02:21,810 --> 00:02:28,320 And I guess in my previous videos I already taught you how do we define a class in case of Cartland. 33 00:02:28,530 --> 00:02:32,040 Now to instantiate this class we can simply use. 34 00:02:32,220 --> 00:02:37,500 Let's say that space student equals to student. 35 00:02:37,530 --> 00:02:44,940 So typically in case of Cartland we define we instantiate our class by using that espies student equal 36 00:02:44,940 --> 00:02:45,800 to student. 37 00:02:46,020 --> 00:02:49,850 Now notice here we don't have any new keyword unlike in Java. 38 00:02:50,100 --> 00:02:50,900 Right. 39 00:02:51,180 --> 00:02:55,280 So the syntax is more cleaner and more precise compared to Java. 40 00:02:55,590 --> 00:03:02,550 And now suppose student has some name so far that inside the Student class as a part of Bardy we can 41 00:03:02,550 --> 00:03:07,110 simply define some field variables which in Cartland is known as property. 42 00:03:07,110 --> 00:03:12,480 So here I will simply use that name which is of their type of string. 43 00:03:12,480 --> 00:03:19,780 Now here it will show some error that the var name must be initialized or should be declared as abstract. 44 00:03:19,800 --> 00:03:25,930 So here I will simply give it a default name as let's say dummy let's call it our domain name. 45 00:03:26,010 --> 00:03:32,310 And now here I will simply initialize this property inside the main method by simply using object names 46 00:03:32,310 --> 00:03:40,900 student Dorte let's say name equal to and which is my name really cool. 47 00:03:41,200 --> 00:03:46,570 And now if I want to print the name inside the main method I will simply write up re-instatement name 48 00:03:46,600 --> 00:03:56,550 as let's see Dollard in the pollution level that could open and close student Dollard name. 49 00:03:56,590 --> 00:04:03,100 So if I turn the court right now I will get and in the output with this statement but I. 50 00:04:03,200 --> 00:04:06,680 So here I get student has got a name as sharing. 51 00:04:06,980 --> 00:04:13,610 So here you can notice I have simply created the object of the Student class and and define the property 52 00:04:13,730 --> 00:04:17,670 or the field variable as name equal to dummy which is their default value. 53 00:04:17,840 --> 00:04:23,410 And here I have simply overridden the name of the student as Cherian and here as simply put in good 54 00:04:23,410 --> 00:04:24,720 order statement. 55 00:04:24,820 --> 00:04:29,310 Now now suppose if I want to print this statement inside the class. 56 00:04:29,330 --> 00:04:29,950 Student. 57 00:04:30,110 --> 00:04:31,230 So what do I do. 58 00:04:31,370 --> 00:04:35,280 So here I will simply create in that block. 59 00:04:35,600 --> 00:04:43,070 Right now this block is immediately executed after the creation of the instance of the Student class. 60 00:04:43,100 --> 00:04:48,000 So when the statement is executed this in block will be called right after. 61 00:04:48,020 --> 00:04:53,620 So inside this I would simply code this statement from here and paste it here. 62 00:04:53,810 --> 00:04:58,660 Right now here if you notice the student is not defined inside the class. 63 00:04:58,670 --> 00:05:00,000 Student Right. 64 00:05:00,110 --> 00:05:04,880 Because the school of student is actually applicable inside the main method here. 65 00:05:04,930 --> 00:05:05,560 Right. 66 00:05:05,600 --> 00:05:07,640 So here we do I would simply remove it. 67 00:05:07,970 --> 00:05:11,310 And now here I want to print this name. 68 00:05:11,630 --> 00:05:16,570 So far that what I really do is this student name has been defined here. 69 00:05:16,750 --> 00:05:24,320 Now what if if I pass this value as a metre inside the constructor like we do in case of Java. 70 00:05:24,320 --> 00:05:28,270 So in case of Codlin we have the concept of primary constructor. 71 00:05:28,470 --> 00:05:35,820 So let's say constructor and simply here declared name call on string. 72 00:05:36,110 --> 00:05:41,420 So here I will simply pass this shrink as a bit I get it. 73 00:05:41,740 --> 00:05:43,940 And now there does remove it. 74 00:05:44,260 --> 00:05:47,430 And also I guess I can command this name from here. 75 00:05:47,500 --> 00:05:48,160 Right. 76 00:05:48,400 --> 00:05:53,790 So inside the block I can simply put in the name as Dollard name. 77 00:05:54,820 --> 00:05:58,450 Which is basically this incoming name from this constructor. 78 00:05:58,510 --> 00:05:58,950 Right. 79 00:05:59,200 --> 00:06:00,440 And now here. 80 00:06:00,520 --> 00:06:02,630 As for the Cartland coring standard. 81 00:06:02,830 --> 00:06:08,430 If you do not specify any more fires such as are they inject or something like that. 82 00:06:08,440 --> 00:06:16,010 So here you can simply skip this constructor keyword and appended to the header so our code is now looking 83 00:06:16,010 --> 00:06:16,940 more cleaner. 84 00:06:17,180 --> 00:06:22,500 So this is actually the header along with the primary constructor defined here. 85 00:06:22,700 --> 00:06:28,910 So as soon as this statement will be executed we will get student has got a name of 3 in the output 86 00:06:28,910 --> 00:06:29,780 console. 87 00:06:29,810 --> 00:06:31,990 So they're just out in the cold. 88 00:06:32,050 --> 00:06:36,350 So again in the output we got student has gotten him a string. 89 00:06:36,480 --> 00:06:37,170 Right. 90 00:06:37,210 --> 00:06:42,790 And now suppose I want to print the name of the student offered this statement that is inserted the 91 00:06:42,790 --> 00:06:44,580 main method somewhere here. 92 00:06:44,620 --> 00:06:45,120 Right. 93 00:06:45,190 --> 00:06:53,440 So far they really do is I would simply use student dord name but here if you notice this name is actually 94 00:06:53,440 --> 00:06:54,210 undefined. 95 00:06:54,430 --> 00:06:59,280 But you must be thinking we have simply specified this name as a meter here. 96 00:06:59,440 --> 00:07:03,200 So why aren't we access it here right inside the main method. 97 00:07:03,220 --> 00:07:08,460 This is simply because this name string is actually just acting as a paid meter. 98 00:07:08,470 --> 00:07:13,670 This is not the property of the class student that we had earlier in this class. 99 00:07:13,720 --> 00:07:14,390 Right. 100 00:07:14,710 --> 00:07:22,750 So similar to any other constructor as for example in case of Java you can simply define name here and 101 00:07:22,750 --> 00:07:28,660 now inside init method which is actually the part of the constructor you can see the primary constructor 102 00:07:29,290 --> 00:07:33,190 this name can be initialized as name equal to name. 103 00:07:33,220 --> 00:07:35,470 And now this name is actually this name. 104 00:07:35,470 --> 00:07:39,040 So we have to tell the compiler by using this keyword. 105 00:07:39,080 --> 00:07:45,570 So this name which is basically this name has been now initialized to this name in-coming. 106 00:07:45,650 --> 00:07:46,310 Right. 107 00:07:46,360 --> 00:07:47,740 Which is actually string. 108 00:07:47,830 --> 00:07:49,590 So finally this. 109 00:07:49,630 --> 00:07:53,740 That name is now has now got the value of sharing. 110 00:07:53,980 --> 00:08:00,800 So inside the main method and now we can simply access student dork name and can simply print the value. 111 00:08:00,880 --> 00:08:01,790 But it is simple. 112 00:08:02,020 --> 00:08:05,960 So if you copy this statement and suppose paste it here. 113 00:08:08,120 --> 00:08:14,540 And now he had to simply put it into Felician student Dorte name. 114 00:08:14,750 --> 00:08:19,330 Then this could simply appear in the value of St and from the main method itself. 115 00:08:19,520 --> 00:08:25,820 And now just to overwrite some confusion let us the name this name as underscored and here as well you 116 00:08:25,820 --> 00:08:29,330 can rename it to underscore and here if you want. 117 00:08:29,330 --> 00:08:35,120 You can also to underscore here that will simply refer to this name variable. 118 00:08:35,120 --> 00:08:42,490 Or you can simply use Dolor name that is actually pointing to this name property variable. 119 00:08:42,670 --> 00:08:49,180 And now ask what our current example we are actually assigning the value to this name variable in two 120 00:08:49,180 --> 00:08:49,800 steps. 121 00:08:49,840 --> 00:08:54,080 That is we are simply passing the name string which comes here. 122 00:08:54,340 --> 00:08:59,440 And then inside the end block we are simply assigning a value to this name. 123 00:08:59,870 --> 00:09:06,850 Now what if if I simply tell you that you can simply declare this variable name property right inside 124 00:09:06,970 --> 00:09:08,550 the primary constructor. 125 00:09:08,740 --> 00:09:14,340 So what I'm talking about is you can simply call a valid space name. 126 00:09:14,530 --> 00:09:15,160 Right. 127 00:09:15,160 --> 00:09:16,940 So here they're discovering this. 128 00:09:17,260 --> 00:09:23,730 And here if you notice we have simply declared a property inside the primary constructor itself. 129 00:09:23,980 --> 00:09:30,130 So instead of doing it in their step we have just done it in one step by simply using the Varrick keyword 130 00:09:30,820 --> 00:09:32,570 inside the primary constructor. 131 00:09:32,760 --> 00:09:38,500 Or if you want the immutable variable then simply use Val here like this. 132 00:09:38,500 --> 00:09:43,760 So the primary constructor simply supports Val and also that as well. 133 00:09:45,060 --> 00:09:50,860 So this can be removed from here and now our code will now again work perfectly fine. 134 00:09:50,870 --> 00:09:52,960 Let us read the code. 135 00:09:53,130 --> 00:09:56,230 So again we got student has got a name as string. 136 00:09:56,460 --> 00:09:58,470 And here let us remove this comment. 137 00:09:58,470 --> 00:10:01,500 This will make our code look more cleaner. 138 00:10:01,950 --> 00:10:07,950 So this was all about this video where we learned how to declare a class the primary constructor and 139 00:10:07,950 --> 00:10:13,620 also we learned about the in it block and along with this we also learned about the property of the 140 00:10:13,620 --> 00:10:19,130 class and also we can declare the property right here inside the class Vardi. 141 00:10:19,140 --> 00:10:21,360 So this was all about this video. 142 00:10:21,360 --> 00:10:23,140 Thanks for watching and have a good day.