1 00:00:01,370 --> 00:00:02,580 Hi this is so hard. 2 00:00:02,580 --> 00:00:04,540 Once again welcome to the next video. 3 00:00:04,530 --> 00:00:07,840 All this object oriented programming in case of Cartland. 4 00:00:07,980 --> 00:00:14,820 Now in the previous video we learned about the properties primary constrictors in a blog and so on. 5 00:00:14,820 --> 00:00:16,350 Now in this video let us know. 6 00:00:16,350 --> 00:00:20,740 Check out the concept of secondary constrictors in case of Cartland. 7 00:00:20,790 --> 00:00:26,870 So unlike Java in case of Codlin we have the concept of secondary constructors as well. 8 00:00:26,910 --> 00:00:32,940 So here what we do is inside the same application that we created in the previous VDU inside the class 9 00:00:32,940 --> 00:00:33,690 of student. 10 00:00:33,870 --> 00:00:37,510 What we do is I would simply declare a secondary constructor. 11 00:00:37,890 --> 00:00:44,370 Now to declare a second constructor you have to simply use the keyword of constructor followed by simply 12 00:00:44,370 --> 00:00:45,980 defined update on Wieters. 13 00:00:45,980 --> 00:00:48,910 Now if you are declaring that secondary constructor. 14 00:00:48,960 --> 00:00:55,560 Then make sure you give an explicit call to this primary constructor as well because the compiler won't 15 00:00:55,620 --> 00:01:01,580 allow to write such code without calling this primary constructor from the secondary constructor. 16 00:01:01,710 --> 00:01:11,250 No what I mean is you have to simply declare such as name string and then let us use the Demetre of 17 00:01:11,370 --> 00:01:12,330 ID. 18 00:01:12,330 --> 00:01:20,580 Let's see in now followed by a colon followed by a simply give a call to this primary constructor by 19 00:01:20,580 --> 00:01:22,940 simply passing the name right. 20 00:01:23,310 --> 00:01:31,410 And now unlike the primary constructor the second constructor has its own body so the primary constructor 21 00:01:31,590 --> 00:01:33,810 does not contain its own body. 22 00:01:33,990 --> 00:01:41,000 But you can treat this in a blog as a part of this primary constructor that we saw in the previous value. 23 00:01:41,160 --> 00:01:44,190 So this second preconstructed has its own body. 24 00:01:44,190 --> 00:01:51,180 And here you have to simply call this primary constructor because without calling this primary constructor 25 00:01:51,270 --> 00:01:52,320 Let's see what happens. 26 00:01:52,330 --> 00:01:54,010 It simply shows some error. 27 00:01:54,150 --> 00:01:57,180 The primary constructor call is actually expected. 28 00:01:57,240 --> 00:02:03,510 So with the help of this keyword you can simply call this primary constructor that will simply instantiate 29 00:02:03,630 --> 00:02:06,060 this field variable of name. 30 00:02:06,060 --> 00:02:10,290 Now it is not mandatory to keep this name and this name as. 31 00:02:10,350 --> 00:02:12,340 You can also call it let's see. 32 00:02:12,340 --> 00:02:13,960 And so here. 33 00:02:13,980 --> 00:02:20,500 Simply give a call to and right now correctly if you notice this ID is just acting as up. 34 00:02:20,510 --> 00:02:22,970 Then we go inside the secondary constructor. 35 00:02:23,220 --> 00:02:29,360 Unlike this name which is actually the part of the class and behaving as a property of the class. 36 00:02:29,460 --> 00:02:32,020 But this is not the property of the class. 37 00:02:32,220 --> 00:02:35,270 So far that latest append that keyword. 38 00:02:35,460 --> 00:02:36,820 And let's see what happens. 39 00:02:36,870 --> 00:02:43,160 So the intelligent ID is showing that that on the second constructed is not allowed as a parameter. 40 00:02:43,380 --> 00:02:49,410 So in Gartley you can not declare the properties inside the secondary constructor unlike the primary 41 00:02:49,410 --> 00:02:53,370 constructor where you have declared name as a property of the class. 42 00:02:53,460 --> 00:02:57,810 So this VAD or Val is not allowed inside the secondary constructor. 43 00:02:58,140 --> 00:03:00,500 So what is the solution for this. 44 00:03:00,510 --> 00:03:06,800 So for this we have to simply declare that Id equal to c minus one. 45 00:03:06,840 --> 00:03:09,810 This is our default value assigned to this ID. 46 00:03:09,950 --> 00:03:13,060 Now let's just declare though type of type. 47 00:03:13,200 --> 00:03:16,190 Let's see in. 48 00:03:16,370 --> 00:03:22,100 So now we have simply defined the property of the student as that ID is equal to minus 1 which is again 49 00:03:22,100 --> 00:03:23,620 their default value. 50 00:03:23,810 --> 00:03:28,320 So inside the body of the second constructor we can simply use this keyword. 51 00:03:28,370 --> 00:03:32,920 This dog i.e. equal to the incoming ID. 52 00:03:33,150 --> 00:03:37,310 And now you can access this ID inside the block. 53 00:03:38,060 --> 00:03:46,010 And now inside the main method you can simply use student object door ID and simply print the ID of 54 00:03:46,020 --> 00:03:46,660 the student. 55 00:03:46,750 --> 00:03:49,360 There does use the print keyword and let us know. 56 00:03:49,360 --> 00:03:54,680 Also pass this coma and not previously when the statement was getting executed. 57 00:03:54,680 --> 00:03:57,570 It was simply going to call this primary constructor. 58 00:03:57,680 --> 00:04:04,400 But suppose if you have got the value of the student id then in that case simply call the secondary 59 00:04:04,400 --> 00:04:10,070 constructor and by simply passing to it meters like say the ID of the student is now then. 60 00:04:10,270 --> 00:04:17,620 So when the statement will be executed it will simply call this second preconstructed where N will get 61 00:04:17,620 --> 00:04:23,980 the value of student and this ID will not be done and explicitly we are calling the primary constructor 62 00:04:24,190 --> 00:04:30,670 simply pass the value of a string that will be now stored inside of that name. 63 00:04:30,910 --> 00:04:38,080 And then we will print the statement as student has got a name as string and inside the secondary constructor 64 00:04:38,170 --> 00:04:42,000 we are simply initializing property of ID as ID. 65 00:04:42,070 --> 00:04:42,850 And now here. 66 00:04:42,880 --> 00:04:45,240 I'm simply painting the student ID. 67 00:04:45,250 --> 00:04:46,240 Let us note in the chord 68 00:04:49,200 --> 00:04:50,110 and not in the output. 69 00:04:50,170 --> 00:04:55,320 And so you can see student has gotten the name as she and and this is the idea of the student. 70 00:04:55,320 --> 00:05:01,360 Now you must be thinking why did not I bring the student ID inside the upper end Elena statement. 71 00:05:01,360 --> 00:05:01,900 We didn't. 72 00:05:01,920 --> 00:05:04,940 And it mattered little ride this out as well. 73 00:05:05,190 --> 00:05:06,040 Let's see. 74 00:05:06,300 --> 00:05:11,590 An 80 is dollar ID. 75 00:05:11,820 --> 00:05:14,550 Let us know in the court again. 76 00:05:14,630 --> 00:05:21,110 And now this time I have simply got this outward student has got the name string and the ID is minus 77 00:05:21,110 --> 00:05:21,520 one. 78 00:05:21,770 --> 00:05:28,520 And right after this statement when this student is executed we are getting the fake ID ESTAN. 79 00:05:28,560 --> 00:05:35,030 Now why this is being rendered as minus 1 which is actually their default value of the ID. 80 00:05:35,030 --> 00:05:39,380 Now what happens is that when this statement is executed. 81 00:05:39,390 --> 00:05:41,290 Student Cherian. 82 00:05:41,430 --> 00:05:46,430 And so what happens is that it's simply called the second preconstructed. 83 00:05:46,650 --> 00:05:52,590 Now this second three constructor simply called this primary constructor which in turn simply execute 84 00:05:52,680 --> 00:05:54,580 this statement in a block. 85 00:05:54,810 --> 00:06:00,080 Now right after the block is executed this statement is executed. 86 00:06:00,420 --> 00:06:01,370 Right. 87 00:06:01,400 --> 00:06:04,430 So Windus 80 has got the value of 10. 88 00:06:04,620 --> 00:06:10,920 But before that this statement was already exec you dead where the ID was minus one. 89 00:06:10,920 --> 00:06:13,120 So here you can simply write a command line. 90 00:06:13,320 --> 00:06:17,470 The body of the secondary constructor is scored off the block. 91 00:06:17,670 --> 00:06:21,680 So that is why the value of the ID is minus 1 instead of 10. 92 00:06:21,720 --> 00:06:25,700 So always remember this so are the end of this video. 93 00:06:25,720 --> 00:06:32,710 Let us embrace in this video we learn about the second constructor and also we cannot declare the property 94 00:06:32,950 --> 00:06:36,780 like that space inside the second reconstructed. 95 00:06:37,120 --> 00:06:43,030 And if you're declaring the second constructor then you must give a call to the primary constructor 96 00:06:43,120 --> 00:06:44,710 like we have done here. 97 00:06:44,980 --> 00:06:51,120 And this will simply called this constructor present here which is actually the primary constructor. 98 00:06:51,130 --> 00:06:51,890 Fine. 99 00:06:52,000 --> 00:06:55,840 So here let us remove the school from here which is of no use. 100 00:06:55,840 --> 00:06:58,060 So this was all about this video. 101 00:06:58,060 --> 00:06:59,910 Thanks for watching and have a good day.