1 00:00:01,550 --> 00:00:06,900 Heidi's rings at Howard and welcome to the next video of this object oriented programming section. 2 00:00:07,000 --> 00:00:12,070 Now in the previous video we learned the concept of overriding in Gears of Cartland. 3 00:00:12,070 --> 00:00:17,950 Now in this video let us check out how do we use the primary and secondary constrictors when we have 4 00:00:18,010 --> 00:00:19,490 inheritance involved. 5 00:00:21,120 --> 00:00:24,620 So here inside our project I have simply mortified my call. 6 00:00:24,750 --> 00:00:27,290 So this is their derived class of dog. 7 00:00:27,450 --> 00:00:29,970 And this is the superclass of animal. 8 00:00:29,970 --> 00:00:36,440 Now notice here we have the open keyword and inside the Animal class I have the property of color. 9 00:00:36,690 --> 00:00:39,650 And inside the dog I have the property of Breede. 10 00:00:39,990 --> 00:00:45,820 So their dog claws indirectly gets the property variable of color as well. 11 00:00:46,020 --> 00:00:52,030 So inside the main class I'm simply asserting that color as black and dog dog breed eager to pug. 12 00:00:52,230 --> 00:00:55,960 So I guess no doubt they'll hear. 13 00:00:55,990 --> 00:01:02,650 And now suppose instead of defining the property variable here let us use the constructor to define 14 00:01:02,650 --> 00:01:04,050 the property variable. 15 00:01:04,360 --> 00:01:09,820 Let's see that SPEEs breed of their type of string. 16 00:01:10,000 --> 00:01:10,880 Right. 17 00:01:10,920 --> 00:01:11,520 Let us know. 18 00:01:11,530 --> 00:01:15,830 Remove it and now by Bideford inside the constructor as well. 19 00:01:15,840 --> 00:01:17,900 You're going to find their default value. 20 00:01:18,100 --> 00:01:25,020 So this is actually gunas their default constructor very refined their default values for these variables 21 00:01:25,210 --> 00:01:27,660 such as I can define here black. 22 00:01:27,910 --> 00:01:34,100 So if you don't pass any value inside the dog constructor then by default this really will be WILLIGER 23 00:01:34,120 --> 00:01:38,040 the value of black and this concept of before it began Meaders. 24 00:01:38,110 --> 00:01:44,320 We've already seen in the previous more D-Will where we learn about our default meters in case of metrics 25 00:01:44,320 --> 00:01:45,500 in Cartland. 26 00:01:45,520 --> 00:01:48,620 So as of now let us remove it. 27 00:01:48,660 --> 00:01:53,580 So here the compiler is not happy because we have to pass some value here. 28 00:01:53,820 --> 00:01:56,510 So here I would simply correct the bug from here. 29 00:01:57,350 --> 00:02:00,260 And pasted inside the primary constructor of God. 30 00:02:01,130 --> 00:02:02,180 Right. 31 00:02:02,270 --> 00:02:08,090 So when this statement will be executed inside the primary constructor this being a variable will get 32 00:02:08,090 --> 00:02:09,430 the value of. 33 00:02:09,830 --> 00:02:13,930 And now the same thing we can apply for the color variable as well. 34 00:02:14,150 --> 00:02:22,400 So here inside the primary constructor and the holder of any well you can simply define that color of 35 00:02:22,400 --> 00:02:24,460 the type of string. 36 00:02:24,500 --> 00:02:28,120 And now here as well you can define their default value. 37 00:02:28,250 --> 00:02:29,300 Let's see black. 38 00:02:29,330 --> 00:02:29,770 Right. 39 00:02:29,900 --> 00:02:32,090 So as of now let us remove it. 40 00:02:32,540 --> 00:02:35,600 And now let us remove this that color from here. 41 00:02:35,630 --> 00:02:37,030 We don't need it. 42 00:02:37,430 --> 00:02:40,350 So here at the bottom you will find some error. 43 00:02:40,430 --> 00:02:46,550 It simply says that we need to pass some value because the primary constructor of the animal contains 44 00:02:46,770 --> 00:02:47,750 under-paid meter. 45 00:02:47,870 --> 00:02:52,640 So here are they we do is I simply pass some value not data value. 46 00:02:52,670 --> 00:02:56,150 I have to get it from this dog constructor. 47 00:02:56,150 --> 00:02:59,620 So for this I have to modify my doc constructor. 48 00:03:00,130 --> 00:03:01,610 Let's modify it. 49 00:03:01,610 --> 00:03:08,490 Let us define that pyramided as colored string Komal that breede. 50 00:03:08,750 --> 00:03:16,800 So now inside the animal I can simply pass color and now add that all I am having some added h simply 51 00:03:16,800 --> 00:03:18,320 expects to be 10 meters. 52 00:03:18,330 --> 00:03:21,410 And now I have one bit I need to define. 53 00:03:21,540 --> 00:03:28,270 So here as a first bit I could simply pass black Come on X and feasted here going up. 54 00:03:30,710 --> 00:03:31,350 And now. 55 00:03:31,460 --> 00:03:37,460 So what is happening here is that when this deal will be executed it will simply call the primary constructor 56 00:03:37,550 --> 00:03:39,190 of this dog plus. 57 00:03:39,350 --> 00:03:42,040 So here is our primary constructor. 58 00:03:42,050 --> 00:03:49,580 So inside this deescalated meter we will get the value of black and this property variable of breed 59 00:03:49,690 --> 00:03:51,580 will get the value of. 60 00:03:52,030 --> 00:03:55,840 And now in return I'm calling the superclass constructor. 61 00:03:56,120 --> 00:04:03,830 That is the Animal class constructor where the value of color is actually this one right which is actually 62 00:04:03,830 --> 00:04:04,610 black. 63 00:04:04,610 --> 00:04:09,350 So this statement will simply pass in the value of black which will fall here. 64 00:04:09,440 --> 00:04:12,660 Add the property variable of this class of animal. 65 00:04:12,980 --> 00:04:20,260 So here inside the class of dog let us do fine our innate block and it has some value. 66 00:04:20,420 --> 00:04:26,070 And now let us copy this block from here and pasted inside the animal as well. 67 00:04:26,750 --> 00:04:32,060 And are just mortified I called animal and here removed the read. 68 00:04:32,220 --> 00:04:33,260 Right. 69 00:04:34,240 --> 00:04:38,400 And now let us run the code and let's see if everything is looking fine. 70 00:04:40,840 --> 00:04:47,360 So here we go in the output console we are getting from animal in it black from dog in it black and 71 00:04:47,410 --> 00:04:48,130 white. 72 00:04:48,490 --> 00:04:55,200 So what is exactly happening is that when the state is executed it simply triggers this method or just 73 00:04:55,210 --> 00:05:02,950 constructor which in turn simply cause the super constructor animal and simply pass it as a value deadfalls 74 00:05:03,070 --> 00:05:06,950 here and has regained the value of the Kalid inside the animal. 75 00:05:06,970 --> 00:05:12,270 Plus and in that scene we hear we get the value of the breed. 76 00:05:12,360 --> 00:05:13,020 Right. 77 00:05:13,080 --> 00:05:15,510 And here we are finding the values. 78 00:05:15,510 --> 00:05:21,660 So in this way when we use constructor's in case of inheritance we have to compulsorily follow this 79 00:05:21,660 --> 00:05:22,480 guideline. 80 00:05:22,650 --> 00:05:25,660 Always call the superclass primary constructor. 81 00:05:25,740 --> 00:05:30,900 If you are having it right and now at the end just to avoid confusion. 82 00:05:30,930 --> 00:05:33,400 Always remember that sequence. 83 00:05:33,450 --> 00:05:36,750 So here the first bit I mean there is actually lot here. 84 00:05:36,810 --> 00:05:43,520 So in there derived class as well the first fit I did always make it color just to avoid any confusion. 85 00:05:43,710 --> 00:05:50,130 Now if you are in multiple constructors in your class such as if you want to use the secondary constructor 86 00:05:50,190 --> 00:05:53,130 instead of primary constructor then let's see. 87 00:05:53,130 --> 00:05:53,920 Var genius. 88 00:05:53,930 --> 00:05:55,160 Do we have to make. 89 00:05:55,500 --> 00:06:01,830 So here let us remove the in it and block and again it doesn't move it from here. 90 00:06:02,170 --> 00:06:10,140 And no I was simply defined my second reconstructed and what I would do is I would simply correct these 91 00:06:10,460 --> 00:06:11,670 meters from here. 92 00:06:12,710 --> 00:06:15,670 And thought X and pasted right here. 93 00:06:16,970 --> 00:06:19,380 And let us remove the declaration here. 94 00:06:21,040 --> 00:06:24,960 And now the second constructor has its own body. 95 00:06:25,340 --> 00:06:26,320 And here as well. 96 00:06:26,360 --> 00:06:32,910 I will create a second constructor and let us go to this statement from here. 97 00:06:33,910 --> 00:06:40,240 And based it inside the animal constructed right and it has its own body. 98 00:06:40,540 --> 00:06:46,810 And now as for the rule the second constructor cannot contain the valid keyword that is you can not 99 00:06:46,810 --> 00:06:49,990 declare the properties inside the secondary constructor. 100 00:06:49,990 --> 00:06:58,340 So what I will do is I will simply copy it and define it as a property inside this that I like this. 101 00:06:58,430 --> 00:07:03,230 And now I would simply remove the very key word from here and inside the constructor I would simply 102 00:07:03,230 --> 00:07:10,760 use this dark color equal to it so as to initialize our color variable from here. 103 00:07:10,820 --> 00:07:11,620 Right. 104 00:07:11,900 --> 00:07:13,780 And now here it shows some error. 105 00:07:13,880 --> 00:07:16,360 The primary constructor call expected. 106 00:07:16,370 --> 00:07:23,840 So here we need to simply use galloon SPEEs this but I give a call to the primary constructor as well. 107 00:07:23,900 --> 00:07:24,780 This one. 108 00:07:25,310 --> 00:07:28,600 And now inside the class dog again we have some error. 109 00:07:28,910 --> 00:07:32,420 So what we do is let us move this color from here. 110 00:07:33,910 --> 00:07:40,690 And now inside this we need to remove this that keyword because we can not have the properties inside 111 00:07:40,780 --> 00:07:42,570 the secondary constructor. 112 00:07:42,850 --> 00:07:50,090 So let us define the VAT breed of that type of string equal to let's say a blank string. 113 00:07:50,440 --> 00:07:56,330 And now inside the constructor I will again use this dot be equal to B. 114 00:07:56,650 --> 00:07:57,270 Right. 115 00:07:57,400 --> 00:08:00,620 And now aspartate it was defined inside the Cartland. 116 00:08:01,060 --> 00:08:07,690 Whenever we are using the secondary constructor we need to call the superclass constructor as well. 117 00:08:07,690 --> 00:08:11,160 So from this constructor we need to call this constructor. 118 00:08:11,320 --> 00:08:18,980 So here what they would do is I would simply use galloon and then call super And as a filmmaker I would 119 00:08:18,980 --> 00:08:24,090 simply pass this color so that as far as this Galit right. 120 00:08:24,290 --> 00:08:29,500 So this super statement will simply call this constructor defined here. 121 00:08:30,020 --> 00:08:31,800 And here it is showing some error. 122 00:08:31,880 --> 00:08:36,630 Let us do one thing and move it from here and also from here. 123 00:08:36,920 --> 00:08:38,710 So this rectifies our code. 124 00:08:38,810 --> 00:08:44,870 So in case of second preconstructed What do we have here is this is our last dog that inherits all the 125 00:08:44,870 --> 00:08:46,820 properties from the animal class. 126 00:08:46,840 --> 00:08:47,420 Right. 127 00:08:47,690 --> 00:08:50,190 And here we have defined a secondary constructor. 128 00:08:50,240 --> 00:08:55,740 So when the statement the top is exec you did this. 129 00:08:55,800 --> 00:09:03,000 This constructor is actually triggered so will get the value of that and we will get the value of. 130 00:09:03,330 --> 00:09:09,930 And this statement in return will simply trigger the superclass constructor that is defined here right 131 00:09:10,140 --> 00:09:13,230 which in turn was simply initialized the color variable. 132 00:09:13,320 --> 00:09:18,040 And again since we have removed the primary constructor declaration from here. 133 00:09:18,210 --> 00:09:21,700 So we don't need to call the primary constructor from here. 134 00:09:21,930 --> 00:09:23,160 So it doesn't remove it. 135 00:09:23,340 --> 00:09:25,210 And this rectifies our error. 136 00:09:25,350 --> 00:09:27,050 And now let us do one thing. 137 00:09:27,080 --> 00:09:35,090 Let us try to put in some values like say inside the constructor and let us copy this statement and 138 00:09:35,090 --> 00:09:38,070 pasted inside this construct as well. 139 00:09:38,420 --> 00:09:43,750 And here I would simply use from animal as so check if we are getting the values or not. 140 00:09:44,210 --> 00:09:46,330 And now let us read the code. 141 00:09:48,950 --> 00:09:55,030 So again in the output we get from animala class Vigurs the output as black and insert dog. 142 00:09:55,040 --> 00:09:58,960 We're getting the value of black and pug respectively. 143 00:09:59,090 --> 00:10:04,400 So our code is now working perfectly fine and each plus is getting a student value with the help of 144 00:10:04,400 --> 00:10:07,340 constrictors in Garling right. 145 00:10:08,730 --> 00:10:14,640 So in this video we learn about how do we use the primary constrictors and also the secondary constrictors 146 00:10:14,670 --> 00:10:15,870 in case of Cartland.