1 00:00:00,930 --> 00:00:01,890 ‫Welcome back. 2 00:00:01,920 --> 00:00:07,210 ‫In this video, I would like to talk about sealed methods or in general, the seal keyword. 3 00:00:07,230 --> 00:00:15,690 ‫So if I want to override a method, I can simply make it virtual in the base clause and then override 4 00:00:15,690 --> 00:00:18,760 ‫it in the deriving clause. 5 00:00:18,780 --> 00:00:20,070 ‫So we have seen that. 6 00:00:20,070 --> 00:00:22,740 ‫But what if I have another class? 7 00:00:22,740 --> 00:00:24,210 ‫Can I do that even further? 8 00:00:24,270 --> 00:00:33,180 ‫So let's say I create a new class here, add class and I'm going to call that 1m3, which is going to 9 00:00:33,180 --> 00:00:39,210 ‫do to be an BMW M3, so a subclass of BMW or a driving class. 10 00:00:39,210 --> 00:00:45,480 ‫So it means we need to use BMW here and now we need to create a constructor. 11 00:00:45,480 --> 00:00:48,150 ‫So I'm just going to copy the one from there. 12 00:00:49,140 --> 00:00:53,190 ‫And here I'm also going to add the model. 13 00:00:54,570 --> 00:00:57,210 ‫So at this one is going to be the M three. 14 00:00:57,210 --> 00:00:58,470 ‫So now it's not complaining. 15 00:00:58,530 --> 00:01:02,190 ‫I just added that so that it's not complaining because it needs one. 16 00:01:02,190 --> 00:01:04,710 ‫Otherwise we would need to have a default constructor. 17 00:01:04,710 --> 00:01:07,920 ‫Otherwise if we don't want to do that in our base class. 18 00:01:07,920 --> 00:01:12,150 ‫So in here we would need a default constructor as we have seen before. 19 00:01:12,150 --> 00:01:19,680 ‫So car just like that, for example, this is a default constructor, default construct. 20 00:01:21,870 --> 00:01:25,260 ‫And now if I go ahead and get rid of this. 21 00:01:26,730 --> 00:01:27,510 ‫It doesn't. 22 00:01:28,050 --> 00:01:33,720 ‫It's still complaining, but that's because BMW does not have a default constructor as well. 23 00:01:33,720 --> 00:01:36,440 ‫So BMW like that. 24 00:01:36,450 --> 00:01:39,900 ‫And now we can go ahead and get rid of this one here. 25 00:01:41,160 --> 00:01:43,410 ‫As you can see now, it's not complaining anymore. 26 00:01:43,410 --> 00:01:50,400 ‫But as I didn't want to do that and as I didn't want to create all those default constructors, it's 27 00:01:50,400 --> 00:01:52,170 ‫more about the sealed keyword. 28 00:01:52,170 --> 00:01:55,020 ‫So let's say I want to override this method here. 29 00:01:55,020 --> 00:02:02,160 ‫So the BMW method repair, which is already overriding from car now and free, wants to also override 30 00:02:02,160 --> 00:02:02,580 ‫that. 31 00:02:02,580 --> 00:02:12,300 ‫So I'm just going to go ahead and create an override void repair and here it calls the base repair already. 32 00:02:12,300 --> 00:02:17,730 ‫So it's just going to call the base class and then it's repair method. 33 00:02:17,730 --> 00:02:20,820 ‫So in this case, it will simply call this repair method. 34 00:02:20,940 --> 00:02:26,790 ‫So now I can do that, I can override it and I can go ahead and create an object of RM three. 35 00:02:26,790 --> 00:02:40,680 ‫So let's create one and three is going to be my RM three and it's a new M three and I'm going to add 36 00:02:41,160 --> 00:02:44,010 ‫HP, let's say 260. 37 00:02:44,010 --> 00:02:53,730 ‫The color is going to be red and the model is going to be m3c Super Turbo or whatever. 38 00:02:53,850 --> 00:02:54,780 ‫Not a pro car. 39 00:02:54,830 --> 00:02:57,510 ‫So I'm just going to call it RM three Super Turbo. 40 00:02:58,020 --> 00:03:07,080 ‫And now if I call the my RM three dot repair method, it will simply call the base method. 41 00:03:08,980 --> 00:03:12,010 ‫So the BMW Super Turbo was repaired. 42 00:03:13,000 --> 00:03:17,020 ‫So even though I didn't add anything else, I just called this base repair. 43 00:03:17,050 --> 00:03:20,840 ‫Now it's calling the repair method of the BMW. 44 00:03:20,860 --> 00:03:22,750 ‫As you can see here, this one. 45 00:03:23,710 --> 00:03:27,550 ‫Okay, so that's one thing, but what if I want to seal it? 46 00:03:27,580 --> 00:03:35,110 ‫So as we were saying before, if you want to seal it so that this method cannot be overridden anymore, 47 00:03:35,140 --> 00:03:37,840 ‫I can simply add this shield keyword. 48 00:03:37,930 --> 00:03:42,160 ‫Now I need to do that for before a override keyword. 49 00:03:42,250 --> 00:03:43,780 ‫Otherwise it doesn't work. 50 00:03:43,780 --> 00:03:50,170 ‫So you cannot say, okay, I'm going to seal this virtual method because that would be the opposite. 51 00:03:50,170 --> 00:03:54,740 ‫So virtual allows to override, sealed doesn't allow to override. 52 00:03:54,760 --> 00:04:01,450 ‫So if I replace virtual with sealed now repair is also going to complain because it says it cannot be 53 00:04:01,450 --> 00:04:02,890 ‫sealed because it's not an override. 54 00:04:02,890 --> 00:04:11,530 ‫So you can only seal override methods, which means a car over a car has a virtual method called repair. 55 00:04:11,560 --> 00:04:20,110 ‫BMW inherits that method and overwrites it and now it seals it so that my RM three cannot use it anymore. 56 00:04:20,110 --> 00:04:24,340 ‫So as you can see, cannot override inherited member because it's sealed. 57 00:04:24,340 --> 00:04:26,950 ‫So now I cannot override this method anymore. 58 00:04:26,950 --> 00:04:31,600 ‫And that's pretty much what this whole sealed keyword is about. 59 00:04:31,600 --> 00:04:38,680 ‫So you seal from inheritance, so you pretty much don't allow inheritance, and you can do the same 60 00:04:38,680 --> 00:04:41,920 ‫thing not only with methods, but you can also do that with cars. 61 00:04:41,920 --> 00:04:49,270 ‫So let's say I want to seal my BMW class so that it cannot be inherited from then. 62 00:04:49,270 --> 00:04:53,470 ‫This whole class will not be able to inherit from the BMW. 63 00:04:53,470 --> 00:04:57,730 ‫So its success here can derive from a sealed type BMW. 64 00:04:58,450 --> 00:05:01,600 ‫All right, so that's what SEAL does. 65 00:05:01,630 --> 00:05:06,640 ‫It simply closes it up for further inheritance. 66 00:05:07,780 --> 00:05:08,260 ‫All right. 67 00:05:08,260 --> 00:05:15,220 ‫So now you have seen how you can use overwriting, then hiding members with the new keyword, and you 68 00:05:15,220 --> 00:05:21,070 ‫now see how to use the sealed keyword in order to seal overwriting methods.