1 00:00:00,150 --> 00:00:04,170 ‫In the last video you learned about inheritance and the basics of inheritance. 2 00:00:04,170 --> 00:00:09,900 ‫But now let's dive a little deeper and look at several key words that will be relevant when it comes 3 00:00:09,900 --> 00:00:11,070 ‫to inheritance. 4 00:00:11,070 --> 00:00:15,480 ‫And the ones that we're going to look at will be virtual and the override keyword. 5 00:00:15,510 --> 00:00:19,290 ‫Now, you might wonder, why do we have animals here on the screen? 6 00:00:19,290 --> 00:00:22,290 ‫Why does Dennis show a bunch of images of animals? 7 00:00:22,320 --> 00:00:28,740 ‫Well, the thing is, we are going to implement an animal class which will then allow us to inherit 8 00:00:28,740 --> 00:00:34,470 ‫from and animal will be the parent class, which has a couple of skills, such as, well, what can 9 00:00:34,470 --> 00:00:35,430 ‫an animal do? 10 00:00:35,460 --> 00:00:39,240 ‫It can make a sound, it can eat and it can play. 11 00:00:39,900 --> 00:00:43,950 ‫That's at least what we are going to allow the animal class to do. 12 00:00:43,950 --> 00:00:46,460 ‫And of course, an animal can do a lot more. 13 00:00:46,470 --> 00:00:52,740 ‫We'll give it a couple of properties such as the name The Age Property, then if it's hungry, for example, 14 00:00:52,740 --> 00:00:56,150 ‫so only if it's hungry, it will eat, otherwise it won't eat. 15 00:00:56,160 --> 00:01:00,210 ‫So that's really the idea of creating an animal class. 16 00:01:00,210 --> 00:01:04,260 ‫And now you could, of course, go ahead and implement this class by yourself already. 17 00:01:04,260 --> 00:01:10,860 ‫And then we're going to use inheritance in order to not only use the same methods and same properties, 18 00:01:10,860 --> 00:01:19,500 ‫but also override them basically saying, okay, the sound that an animal that is a dog makes is going 19 00:01:19,500 --> 00:01:25,140 ‫to be different to the sound that, for example, an ape makes or a monkey makes or a giraffe makes 20 00:01:25,140 --> 00:01:26,070 ‫or something like that. 21 00:01:26,640 --> 00:01:29,340 ‫So this is basically what we're going to implement. 22 00:01:29,340 --> 00:01:35,250 ‫And by doing that, we are going to learn the different key words such as virtual and the override keyword. 23 00:01:35,250 --> 00:01:37,920 ‫So let's go ahead and create a new project for this. 24 00:01:37,920 --> 00:01:39,750 ‫I'm going to use a console application. 25 00:01:39,750 --> 00:01:43,680 ‫I'm going to call it virtual override demo. 26 00:01:44,070 --> 00:01:44,490 ‫All right. 27 00:01:44,490 --> 00:01:49,530 ‫As a little challenge for you, if you want to try this for yourself, please write a little class that 28 00:01:49,530 --> 00:01:52,440 ‫will be called animal and it will have the properties. 29 00:01:52,440 --> 00:01:54,600 ‫Name age is hungry. 30 00:01:54,780 --> 00:02:03,370 ‫Then of course a constructor, then a method that is called make sounds, eat as well as play so positively 31 00:02:03,390 --> 00:02:07,740 ‫and implement all of those if you want to, because this will just give you a little bit of a practice 32 00:02:07,740 --> 00:02:10,020 ‫to create all of these different methods. 33 00:02:10,020 --> 00:02:16,200 ‫And then we will see how to change them a little bit up so that we can use them properly in inheritance. 34 00:02:17,290 --> 00:02:19,080 ‫All right, so I hope you tried it. 35 00:02:19,090 --> 00:02:25,600 ‫Therefore, of course we can use control k c to create a new class and I'm going to call this one animal 36 00:02:26,380 --> 00:02:27,220 ‫like so. 37 00:02:27,220 --> 00:02:30,850 ‫And now this animal class will have a couple of properties, as I said. 38 00:02:30,850 --> 00:02:38,860 ‫So the name property, the H property as well as the is hungry property and then a simple constructor 39 00:02:38,860 --> 00:02:40,510 ‫that uses all of these properties. 40 00:02:40,510 --> 00:02:42,040 ‫You can see we're just using get set. 41 00:02:42,040 --> 00:02:48,280 ‫By the way, if you want to create a property real quickly, just enter property double tap and then 42 00:02:48,280 --> 00:02:51,820 ‫enter the type, for example, string and then here, for example, name. 43 00:02:51,820 --> 00:02:57,310 ‫So that's how you would do it very quickly, not having to type more than necessary. 44 00:02:57,880 --> 00:02:58,480 ‫All right. 45 00:02:58,480 --> 00:03:04,030 ‫So this is going to be our constructor here with the name the H and the is hungry. 46 00:03:04,030 --> 00:03:08,950 ‫And you can see is hungry is not something that we are going to set to the animal once we create it, 47 00:03:09,100 --> 00:03:13,180 ‫but we're going to set it to true at the beginning by default. 48 00:03:13,180 --> 00:03:16,180 ‫So I'm just setting it to its hungry default. 49 00:03:16,900 --> 00:03:17,230 ‫Okay. 50 00:03:17,470 --> 00:03:23,200 ‫So now I said that I want to have a method that will be called make sounds. 51 00:03:23,200 --> 00:03:24,580 ‫So let's look at this method. 52 00:03:24,670 --> 00:03:29,590 ‫So I'm using an empty virtual method, make sound for other classes to override. 53 00:03:29,800 --> 00:03:37,300 ‫I'm making this method virtual so that it can be overridden by classes that inherit from animal. 54 00:03:37,690 --> 00:03:44,350 ‫And we're going to see later on how to do it because the sound that an animal makes will highly depend 55 00:03:44,380 --> 00:03:46,210 ‫on the animal itself. 56 00:03:46,480 --> 00:03:51,820 ‫So being hungry is something that every animal is sharing because, well, that's how we function. 57 00:03:51,820 --> 00:03:54,340 ‫We need energy and we get that through food. 58 00:03:54,340 --> 00:04:00,640 ‫And in order to motivate ourselves to get food or to eat food, we have the sense of hunger. 59 00:04:00,640 --> 00:04:05,770 ‫But this virtual method that we have here, this makes sound, doesn't have to be empty. 60 00:04:05,770 --> 00:04:08,170 ‫So let's look at this eat method here. 61 00:04:09,340 --> 00:04:12,970 ‫And this eat method will also be a virtual method. 62 00:04:13,240 --> 00:04:18,100 ‫This allows our subclasses to override this method. 63 00:04:18,100 --> 00:04:21,940 ‫So if we want to override the eat method, we can do so, but we don't have to. 64 00:04:21,970 --> 00:04:25,540 ‫The general functionality will be the one that you can see here. 65 00:04:25,540 --> 00:04:27,760 ‫So it will check if the animal is hungry. 66 00:04:27,760 --> 00:04:32,050 ‫If it is, then it will just print out the name and say that it is eating. 67 00:04:32,320 --> 00:04:36,820 ‫And then if it's not hungry, it will say name is not hungry. 68 00:04:36,820 --> 00:04:41,020 ‫So the name is, by the way, the property of our animal. 69 00:04:41,020 --> 00:04:43,210 ‫So this name, property of the animal. 70 00:04:43,990 --> 00:04:49,480 ‫Okay, so that is going to be our method and we're going to make it virtual as well. 71 00:04:49,480 --> 00:04:53,620 ‫And then another virtual method which will be to play. 72 00:04:53,890 --> 00:04:59,890 ‫So this virtual method play which will basically say name is playing on the console. 73 00:05:01,750 --> 00:05:05,380 ‫Now we're using some very simple red line statements. 74 00:05:05,380 --> 00:05:07,570 ‫This could be, of course, a lot more complex code. 75 00:05:07,840 --> 00:05:13,600 ‫So this is just to simplify the whole thing and make it more humanly understandable, so to speak, 76 00:05:13,600 --> 00:05:18,730 ‫because playing and eating and stuff, this is something that we can directly understand because we 77 00:05:18,730 --> 00:05:25,060 ‫have seen it in our lives and we have done it ourselves while as reading data from a database or something 78 00:05:25,060 --> 00:05:25,390 ‫like that. 79 00:05:25,390 --> 00:05:28,330 ‫That's not something that we usually see in real life. 80 00:05:28,360 --> 00:05:28,930 ‫Okay. 81 00:05:29,200 --> 00:05:29,680 ‫All right. 82 00:05:29,680 --> 00:05:34,870 ‫So now let's define our classes that will be derived from animal. 83 00:05:34,870 --> 00:05:38,050 ‫So that will inherit from the animal class. 84 00:05:38,950 --> 00:05:43,930 ‫So let's go ahead and use control K and C or command K and see. 85 00:05:43,930 --> 00:05:48,220 ‫And this will then open up the screen where we can then create a class or alternatively just create 86 00:05:48,220 --> 00:05:48,810 ‫a class. 87 00:05:48,820 --> 00:05:55,630 ‫Otherwise, so I'm going to create a dog class and this dog class will inherit from animal like so. 88 00:05:56,270 --> 00:06:02,480 ‫Now you can see already that there is no argument given that corresponds to the required formal parameter 89 00:06:02,480 --> 00:06:05,230 ‫name of animal name string int. 90 00:06:05,240 --> 00:06:08,450 ‫So we need to create a constructor here. 91 00:06:08,840 --> 00:06:14,750 ‫So let's go ahead and create a simple constructor which will follow the same structure. 92 00:06:14,780 --> 00:06:21,920 ‫It will have the name and the age and it will have well use base name H so that we can use the same 93 00:06:21,920 --> 00:06:23,970 ‫constructor that we have defined that animal. 94 00:06:23,990 --> 00:06:29,840 ‫So now I'm going to use this new property called Happy or is Happy and this will be a boolean. 95 00:06:29,870 --> 00:06:35,260 ‫This will basically just store if the animal in this case the dog is happy. 96 00:06:35,270 --> 00:06:39,920 ‫So it will say true if the dog is happy and dogs are happy animals. 97 00:06:39,920 --> 00:06:42,980 ‫So by default, the dog will be happy. 98 00:06:43,430 --> 00:06:46,010 ‫All right, now let's override the method. 99 00:06:46,340 --> 00:06:51,380 ‫So let's simply override the virtual method, eat. 100 00:06:51,620 --> 00:06:54,980 ‫And therefore we're using this override keyword. 101 00:06:55,460 --> 00:06:59,480 ‫So public like a normal method, but we're adding this override keyword. 102 00:06:59,690 --> 00:07:01,840 ‫So override, void it. 103 00:07:02,030 --> 00:07:03,080 ‫Same to you. 104 00:07:03,080 --> 00:07:08,720 ‫Or similar to what we had here where we also had this method, but here we used a virtual keyword. 105 00:07:08,720 --> 00:07:16,910 ‫So this virtual keyword allows our subclasses, our inheriting classes, so the children to use the 106 00:07:16,910 --> 00:07:20,720 ‫override keyword to basically override the functionality of it. 107 00:07:21,080 --> 00:07:27,020 ‫If you want to still call the base functionality of the parent class of the animal class, in this case 108 00:07:27,020 --> 00:07:35,300 ‫we need to use base, dot it base will say, okay, call the method of the base and the base will be 109 00:07:35,300 --> 00:07:36,590 ‫well you see here animal. 110 00:07:36,590 --> 00:07:42,590 ‫So once you double click on base and it's highlighted, you can see that animal is also highlighted 111 00:07:42,590 --> 00:07:44,710 ‫because animal is your base class. 112 00:07:44,720 --> 00:07:51,230 ‫If you keep the control key held and you click on base, it will jump over to the animal class because 113 00:07:51,230 --> 00:07:52,670 ‫that's the base class. 114 00:07:52,940 --> 00:07:58,100 ‫And you can see there are three references of our base class and that is here this base as well as this 115 00:07:58,100 --> 00:08:00,200 ‫base and this animal here at the top. 116 00:08:00,990 --> 00:08:01,240 ‫Okay. 117 00:08:01,340 --> 00:08:08,720 ‫So at this point, we are basically using our base class here, the base class functionality, we're 118 00:08:08,720 --> 00:08:09,980 ‫not overriding anything. 119 00:08:09,980 --> 00:08:14,530 ‫So basically we're saying a dog is the same way that any other animal would. 120 00:08:14,540 --> 00:08:16,250 ‫So nothing special here. 121 00:08:16,880 --> 00:08:22,350 ‫Now, let's look at the make sound method, because this one is going to be quite different because 122 00:08:22,350 --> 00:08:24,350 ‫the dog has to make sounds right. 123 00:08:24,350 --> 00:08:27,560 ‫And this way of making sounds is going to be woof. 124 00:08:28,310 --> 00:08:32,210 ‫It could be also woof or woof or whatever. 125 00:08:32,240 --> 00:08:39,590 ‫However, your dog likes to make sounds, and this is how in my case the dog will make a sound. 126 00:08:39,680 --> 00:08:43,490 ‫So this will be different to every animal. 127 00:08:43,790 --> 00:08:45,890 ‫So every animal will make a different sound. 128 00:08:46,760 --> 00:08:49,940 ‫And that's why we are overriding our make sound. 129 00:08:51,650 --> 00:08:54,410 ‫Well with well, this makes sound that we had here. 130 00:08:54,410 --> 00:08:59,450 ‫We are overriding it with our individual implementation for our dog. 131 00:09:00,590 --> 00:09:05,780 ‫And now let's do the same thing with play because the dog only wants to play if it's happy. 132 00:09:06,140 --> 00:09:08,030 ‫So let's assume that that's the case. 133 00:09:08,300 --> 00:09:13,610 ‫So here, if we look at play, it says Dog is playing, but I'm not happy with this result. 134 00:09:13,610 --> 00:09:16,580 ‫Let's say a dog will only play if it is a happy dog. 135 00:09:16,610 --> 00:09:22,910 ‫So what we can do is we can override, void, play and check if the dog is happy and only then play. 136 00:09:23,270 --> 00:09:31,820 ‫So we have already overwritten the base animal functionality with the functionality well with our own 137 00:09:31,820 --> 00:09:37,130 ‫functionality for the dog or our unique functionality for the dog where we added the condition. 138 00:09:37,130 --> 00:09:38,500 ‫But this could be anything. 139 00:09:38,510 --> 00:09:39,980 ‫You are not limited to your condition. 140 00:09:39,980 --> 00:09:43,580 ‫This could be like an entirely different kind of implementation. 141 00:09:43,580 --> 00:09:48,500 ‫You don't even need to use base play, so you're totally free to use whatever you want. 142 00:09:48,650 --> 00:09:54,290 ‫For example, in the make sound method you can see we're not using base make sound because well, in 143 00:09:54,290 --> 00:09:58,160 ‫the make sound method there's nothing to be called, so there's nothing interesting here. 144 00:09:58,160 --> 00:10:03,680 ‫But even if there was, we don't have to call base, make sound or base, make place play. 145 00:10:03,710 --> 00:10:07,090 ‫We could implement something entirely different here if we wanted to. 146 00:10:08,750 --> 00:10:11,420 ‫So that will be our dog. 147 00:10:11,930 --> 00:10:19,550 ‫Now, let's actually go ahead and save those two classes and work with them in our program, CSS file. 148 00:10:20,130 --> 00:10:20,390 ‫Okay. 149 00:10:20,420 --> 00:10:27,980 ‫So here, let's first of all create a dog like so I'm going to call the dog, which will be a new dog. 150 00:10:28,900 --> 00:10:33,120 ‫Which will have the name of CIF and the dog will be 15 years old. 151 00:10:33,130 --> 00:10:34,330 ‫So pretty old dog. 152 00:10:34,510 --> 00:10:35,440 ‫And now. 153 00:10:36,390 --> 00:10:41,950 ‫We can write something like so we can access the property of the dog like so. 154 00:10:41,970 --> 00:10:43,860 ‫So red line dog name. 155 00:10:43,860 --> 00:10:51,390 ‫So we can get the name of the dog and the age of the dog using dog dot name and dog age. 156 00:10:52,220 --> 00:10:56,810 ‫And then we can, for example, play and eat. 157 00:10:57,120 --> 00:10:58,570 ‫Okay, let's do that real quick. 158 00:10:58,580 --> 00:11:00,110 ‫Don't play dog eat. 159 00:11:00,320 --> 00:11:01,550 ‫So let's run this. 160 00:11:03,780 --> 00:11:06,180 ‫And we can see Sive is 15 years old. 161 00:11:06,180 --> 00:11:08,220 ‫Safest playing, surface eating. 162 00:11:09,140 --> 00:11:09,430 ‫Okay. 163 00:11:09,440 --> 00:11:10,760 ‫So play and eat work. 164 00:11:10,760 --> 00:11:11,680 ‫Totally fine. 165 00:11:11,690 --> 00:11:15,620 ‫Now, of course, we can also use Dog Dot. 166 00:11:16,670 --> 00:11:17,840 ‫And make sound here. 167 00:11:17,840 --> 00:11:21,020 ‫So this would then make the dog sound. 168 00:11:21,620 --> 00:11:22,520 ‫Let's run this. 169 00:11:23,390 --> 00:11:26,420 ‫And you see the stock, says, Wolf. 170 00:11:27,590 --> 00:11:28,190 ‫All right. 171 00:11:31,410 --> 00:11:37,360 ‫So let's quickly recap and look at how this is different to the example that we had in the last video. 172 00:11:37,380 --> 00:11:44,400 ‫So we have our base class and this base class has a couple of properties and it also has a couple of 173 00:11:44,400 --> 00:11:46,470 ‫methods, functions, so to speak. 174 00:11:46,470 --> 00:11:50,370 ‫So here to make sound, for example, the E method and so forth. 175 00:11:50,610 --> 00:11:58,110 ‫And we make them virtual by adding the virtual keyword in order to make them changeable or override 176 00:11:58,130 --> 00:12:00,660 ‫able in the inheriting class. 177 00:12:00,660 --> 00:12:03,720 ‫So dogs is the inheriting class or the subclass. 178 00:12:03,720 --> 00:12:13,800 ‫And this subclass allows us to now override a specific method from the base class using the override 179 00:12:13,800 --> 00:12:14,610 ‫keyword. 180 00:12:14,790 --> 00:12:16,140 ‫So that will now. 181 00:12:17,120 --> 00:12:18,680 ‫Override the functionality. 182 00:12:18,680 --> 00:12:24,620 ‫So it will replace the functionality or the code of the current method. 183 00:12:25,310 --> 00:12:27,800 ‫Except you of course use this based on it. 184 00:12:27,800 --> 00:12:32,990 ‫So what that will do is it will basically just call the method of the base class, which means of the 185 00:12:32,990 --> 00:12:37,160 ‫parent, but you are free to implement your very own code here. 186 00:12:37,160 --> 00:12:39,260 ‫You don't have to call the base class. 187 00:12:39,740 --> 00:12:45,110 ‫You can of course add conditions so that the base class functionality would be called in a specific 188 00:12:45,110 --> 00:12:45,920 ‫condition. 189 00:12:45,920 --> 00:12:52,880 ‫Or you can write whatever code you want inside of your overriding methods, and then something else 190 00:12:52,880 --> 00:12:53,450 ‫is different. 191 00:12:53,450 --> 00:12:58,910 ‫And that is that we are adding code to our constructor here for our. 192 00:13:00,120 --> 00:13:01,080 ‫Subclass. 193 00:13:01,500 --> 00:13:10,440 ‫So this inheriting class dog is on one hand using the same properties that our base class is using, 194 00:13:10,440 --> 00:13:13,860 ‫but at the same time it has an own implementation. 195 00:13:13,860 --> 00:13:16,830 ‫What should happen once a dog is created? 196 00:13:16,830 --> 00:13:19,260 ‫So once a dog object is created? 197 00:13:19,260 --> 00:13:23,190 ‫And in our case it is just to say that the dog is going to be happy. 198 00:13:26,180 --> 00:13:26,630 ‫All right. 199 00:13:26,630 --> 00:13:27,920 ‫That's it for this video. 200 00:13:27,950 --> 00:13:33,020 ‫We're going to look a lot more into inheritance, of course, because there's still a lot to learn. 201 00:13:33,260 --> 00:13:35,030 ‫So see you in the next video.