1 00:00:00,750 --> 00:00:03,660 ‫Welcome back to the polymorphism example. 2 00:00:03,660 --> 00:00:08,670 ‫In this video, I'm going to use a different approach where you first of all have a little challenge. 3 00:00:08,670 --> 00:00:15,270 ‫You should write some code which will help you to understand inheritance a bit further or a bit better. 4 00:00:15,270 --> 00:00:20,670 ‫And then we will go into the poly forms, make part of this whole program. 5 00:00:20,670 --> 00:00:27,150 ‫So please go ahead and try to write the code according to this challenge here. 6 00:00:28,360 --> 00:00:29,870 ‫So pause the video and try. 7 00:00:32,530 --> 00:00:33,040 ‫All right. 8 00:00:33,040 --> 00:00:34,180 ‫I hope you tried it. 9 00:00:34,180 --> 00:00:40,450 ‫So I'm going to create a new class ed class, and I'm going to call it car. 10 00:00:41,110 --> 00:00:46,690 ‫And this class is just going to be a general car class. 11 00:00:46,690 --> 00:00:49,900 ‫And it can contain or it should contain properties. 12 00:00:49,900 --> 00:00:50,470 ‫Right. 13 00:00:50,470 --> 00:01:00,490 ‫As I said, it should have a property called HP Prop and it should be an integer and HP is going to 14 00:01:00,490 --> 00:01:01,630 ‫be the horsepower. 15 00:01:01,630 --> 00:01:03,670 ‫So how powerful is this car? 16 00:01:03,670 --> 00:01:09,340 ‫And then another property which is of type string and which is the color? 17 00:01:10,790 --> 00:01:16,600 ‫Now what I need next is to have a constructor. 18 00:01:16,610 --> 00:01:23,420 ‫So I'm going to create a public car which takes in the HP and the color. 19 00:01:26,540 --> 00:01:36,590 ‫And here I just set this dot HP as the HP and this dot color as the color. 20 00:01:37,280 --> 00:01:44,840 ‫So this color is going to be our property color and the HP is going to be the HP of our class or of 21 00:01:44,840 --> 00:01:47,430 ‫our object that is created off type car. 22 00:01:47,450 --> 00:01:51,410 ‫Now the next one should be the show details method. 23 00:01:51,590 --> 00:01:56,930 ‫So public void show details. 24 00:01:57,440 --> 00:02:00,950 ‫And in here we just write something onto the console. 25 00:02:00,950 --> 00:02:07,150 ‫So CW, HP is going to be plus HP. 26 00:02:07,970 --> 00:02:14,630 ‫Then we have the color, which should be something like the color. 27 00:02:14,960 --> 00:02:22,010 ‫So of course I could use a different approach where I use string manipulation here, but I'm just going 28 00:02:22,010 --> 00:02:23,330 ‫to keep it like this. 29 00:02:23,330 --> 00:02:24,140 ‫Very simple. 30 00:02:24,140 --> 00:02:32,570 ‫So the next one is a public void and it's called repair. 31 00:02:32,570 --> 00:02:35,480 ‫And we're going to change those two methods later on. 32 00:02:35,480 --> 00:02:36,380 ‫So no worries. 33 00:02:36,380 --> 00:02:38,840 ‫But for now, I wanted to keep them very simple. 34 00:02:38,840 --> 00:02:46,340 ‫With the approaches that we have used so far, that will make it more clear what polymorphism does and 35 00:02:46,340 --> 00:02:49,250 ‫here just says car was repaired. 36 00:02:51,140 --> 00:02:54,170 ‫All right, so that's our car and that's our base class. 37 00:02:54,170 --> 00:03:03,050 ‫And now we need the two other classes, which is our BMW, BMW class. 38 00:03:03,350 --> 00:03:14,270 ‫And in here, as I said, we have an additional property called model and this is a string model. 39 00:03:14,750 --> 00:03:19,280 ‫Now, another aspect was the brand, which is a private member. 40 00:03:19,280 --> 00:03:24,770 ‫So private string brand and it's called BMW. 41 00:03:26,540 --> 00:03:32,240 ‫Now next we need a constructor, so a public BMW. 42 00:03:32,510 --> 00:03:38,450 ‫And before I do that, before I create the constructor, I want to inherit from car. 43 00:03:38,900 --> 00:03:41,450 ‫So now my BMW is going to inherit from car. 44 00:03:41,450 --> 00:03:47,630 ‫And what I need here is the HPP, then the color 45 00:03:49,910 --> 00:03:56,250 ‫and the model and here and comma. 46 00:03:56,390 --> 00:04:06,050 ‫So what I can do is I can set the HPP to the HPP actually I can even go ahead and do the same with the 47 00:04:06,050 --> 00:04:06,860 ‫color. 48 00:04:06,890 --> 00:04:13,160 ‫It's going to be the color and the model which is our property should be the model. 49 00:04:13,490 --> 00:04:19,940 ‫So this one here, there, I use this because I want it for this object and the same thing I can do 50 00:04:19,940 --> 00:04:22,190 ‫here and I can do here. 51 00:04:22,400 --> 00:04:28,580 ‫So even though I don't have created those in here, as you see, I can still use them. 52 00:04:28,580 --> 00:04:32,510 ‫Now, here's something that you haven't seen yet, which is base. 53 00:04:32,510 --> 00:04:39,740 ‫So I can use the base keyword here and then I can simply say, okay, I want to use the HPP and the 54 00:04:39,740 --> 00:04:45,830 ‫color of my base class and then I don't need those two lines anymore so I can even get rid of those. 55 00:04:46,620 --> 00:04:48,860 ‫As you can see now, the error disappears as well. 56 00:04:48,860 --> 00:04:55,100 ‫So now we have created a constructor which uses the base constructor of our base, which means of our 57 00:04:55,100 --> 00:04:57,350 ‫base class, which is our car. 58 00:04:57,680 --> 00:05:02,120 ‫It's just going to use this constructor and it's just going to add the model. 59 00:05:02,270 --> 00:05:08,450 ‫So now we have a very short constructor which does all the job, all the whole job. 60 00:05:08,450 --> 00:05:13,850 ‫So now what I need next is a void show details, as I said. 61 00:05:13,850 --> 00:05:19,730 ‫And in here I just want to show the details and I'm just going to copy the method from here or the entry 62 00:05:19,730 --> 00:05:20,630 ‫of the method. 63 00:05:22,190 --> 00:05:28,220 ‫And in here it's going to be the brand first. 64 00:05:28,220 --> 00:05:35,000 ‫So brand plus brand plus HP. 65 00:05:35,270 --> 00:05:38,450 ‫So now it's just going to show some additional details. 66 00:05:38,450 --> 00:05:43,160 ‫And here, as you see, show details, hides, inherited member car show details, use the new keyword 67 00:05:43,160 --> 00:05:44,630 ‫if hiding was intended. 68 00:05:44,630 --> 00:05:47,150 ‫And that's something that we're going to cover in the next video. 69 00:05:47,150 --> 00:05:53,090 ‫But for now, let's also create the repair method, public void repair. 70 00:05:53,120 --> 00:05:59,540 ‫And in here it should just say something like the BMW 71 00:06:01,610 --> 00:06:05,120 ‫and in here was repaired. 72 00:06:05,150 --> 00:06:08,270 ‫So what is this zero that I have there? 73 00:06:08,270 --> 00:06:09,380 ‫Well, that's the model. 74 00:06:10,470 --> 00:06:17,640 ‫So I just want to know the model of the car and I'm just going to say, let's say BMW, Z, Phi, az4 75 00:06:17,640 --> 00:06:20,280 ‫or MX three or MX five was repaired. 76 00:06:20,280 --> 00:06:22,260 ‫So that's what this should do. 77 00:06:22,260 --> 00:06:27,750 ‫And now I need another one, another class, which is pretty similar to what we have here. 78 00:06:27,750 --> 00:06:30,690 ‫So I'm going to create a new class and I'm going to call it Audi. 79 00:06:32,610 --> 00:06:39,660 ‫And for simplicity, I'm just going to copy most of the stuff here, actually, the whole class, because 80 00:06:39,660 --> 00:06:41,100 ‫it's going to be very similar. 81 00:06:42,090 --> 00:06:50,220 ‫What is different is here that it's an Audi and that we have the Audi as the constructor and of course 82 00:06:50,220 --> 00:06:52,440 ‫it needs to inherit from car. 83 00:06:53,700 --> 00:06:58,320 ‫So now that is done and now comes the polymorphism part. 84 00:06:58,320 --> 00:07:03,570 ‫So we have an Audi which is of type car, we have a BMW which is also of type car. 85 00:07:03,570 --> 00:07:11,400 ‫And we have this base class which is car, and now we can go ahead and create multiple objects of that. 86 00:07:11,520 --> 00:07:14,520 ‫Now it will make a lot more sense as soon as we get there. 87 00:07:14,520 --> 00:07:24,240 ‫So let's go ahead and create a new variable called cars, which is going to be a new list of cars. 88 00:07:24,450 --> 00:07:33,720 ‫And even though this is a list of cars, so of the car class, we can create all these and BMW is in 89 00:07:33,720 --> 00:07:34,020 ‫here. 90 00:07:34,020 --> 00:07:41,370 ‫So just can create, let's say an Audi which is blue and is an A four. 91 00:07:43,410 --> 00:07:57,060 ‫Then we have a new BMW which is, let's say 250 HP and let's say is a red car and it's an MX three. 92 00:08:00,570 --> 00:08:09,720 ‫So that's one part of polymorphism that you can use the subclasses instead of the base class or the 93 00:08:09,720 --> 00:08:16,260 ‫deriving classes instead of the base class when working, for example, with the lists or in general 94 00:08:16,260 --> 00:08:18,270 ‫and by general, you will see what I mean. 95 00:08:18,270 --> 00:08:25,110 ‫So what I'm going to I'm going to do is I'm going to run a for each loop, which is going to check all 96 00:08:25,110 --> 00:08:28,740 ‫my cars in my cars list. 97 00:08:29,160 --> 00:08:34,650 ‫And it's just going to write something like car repair or it's going to call a repair method. 98 00:08:36,800 --> 00:08:39,000 ‫All right, so let's call that or let's run that. 99 00:08:39,020 --> 00:08:43,340 ‫But before we do that, we need to console the right key. 100 00:08:45,150 --> 00:08:47,850 ‫Because otherwise our program will simply close. 101 00:08:47,880 --> 00:08:48,900 ‫So there you are. 102 00:08:48,930 --> 00:08:51,060 ‫Car was repaired. 103 00:08:51,390 --> 00:08:55,860 ‫So now it's calling the car method here. 104 00:08:56,250 --> 00:08:57,810 ‫Which which is this one here? 105 00:08:57,810 --> 00:08:59,570 ‫Car was repaired. 106 00:08:59,580 --> 00:09:06,360 ‫So it's not calling this repair method and neither does it call this repair method, which was the BMW 107 00:09:06,360 --> 00:09:12,210 ‫was repaired or here it's the actually here it should be audio or Audi. 108 00:09:12,750 --> 00:09:14,130 ‫Audi was repaired. 109 00:09:14,880 --> 00:09:22,260 ‫So now why does it call the repair method of our car and not the one of BMW and and the Audi one that 110 00:09:22,260 --> 00:09:24,350 ‫is has to do with two things. 111 00:09:24,360 --> 00:09:29,430 ‫So one of them is we have this void repair, which is not a virtual one. 112 00:09:29,430 --> 00:09:37,260 ‫So if we make it virtual void repair, what we now can do is, for example, here we can override it. 113 00:09:37,890 --> 00:09:40,500 ‫So now we can override the repair method. 114 00:09:40,710 --> 00:09:43,200 ‫What that will do is if we run the code again. 115 00:09:46,030 --> 00:09:46,510 ‫We will see. 116 00:09:46,540 --> 00:09:47,380 ‫Car was repaired. 117 00:09:47,380 --> 00:09:49,540 ‫That's our Audi, the first one. 118 00:09:49,540 --> 00:09:54,970 ‫And then the BMW M three was repaired and that is the second one. 119 00:09:54,970 --> 00:10:02,470 ‫And that is because our BMW class here has overridden the repair method. 120 00:10:03,250 --> 00:10:10,870 ‫So we can simply take this method, make it virtual, and that allows us to override it in the deriving 121 00:10:10,870 --> 00:10:11,550 ‫classes. 122 00:10:11,560 --> 00:10:13,000 ‫And that's what we have done here. 123 00:10:13,780 --> 00:10:17,290 ‫So now there are the two aspects of polymorphism. 124 00:10:17,290 --> 00:10:24,400 ‫So polymorphism network one and ODI and VW, Porsche, whatever different classes you want to have can 125 00:10:24,400 --> 00:10:27,640 ‫all be used wherever a car is expected. 126 00:10:27,640 --> 00:10:36,760 ‫So whenever they are inheriting from this class here, from our car class here, this one, we can simply 127 00:10:36,760 --> 00:10:37,570 ‫use it instead. 128 00:10:37,570 --> 00:10:44,740 ‫And then the second aspect of polymorphism is that the virtual method can be invoked on each of the 129 00:10:44,740 --> 00:10:47,350 ‫deriving classes, not on the base class. 130 00:10:47,350 --> 00:10:53,860 ‫So that's something that we have done with the BMW and now we can do the same thing with our order here. 131 00:10:53,860 --> 00:10:59,020 ‫So I'm just going to make that an override as well. 132 00:10:59,410 --> 00:11:00,990 ‫Override, void, repair. 133 00:11:01,100 --> 00:11:02,470 ‫Now let's run it again. 134 00:11:02,740 --> 00:11:08,170 ‫And now both should show their brands and their models. 135 00:11:08,170 --> 00:11:12,850 ‫So we see the Audi A4 and the BMW M3 were repaired. 136 00:11:13,540 --> 00:11:15,880 ‫There is one more thing about polymorphism, though. 137 00:11:15,880 --> 00:11:22,870 ‫So if you want to hide the base class members with new members, you can use the new keyword. 138 00:11:22,870 --> 00:11:31,150 ‫So let's say we have this void show details method in our class car, which is just a void. 139 00:11:31,150 --> 00:11:32,590 ‫It's not a virtual void. 140 00:11:32,620 --> 00:11:39,790 ‫And now if I would like to override that, so not override in the sense that we have done here with 141 00:11:39,790 --> 00:11:40,780 ‫the override one. 142 00:11:40,780 --> 00:11:49,390 ‫But I actually want to use this method instead whenever I'm using a BMW and not in the sense of overriding 143 00:11:49,390 --> 00:11:55,270 ‫the old one, but just hiding the one from car and using show details instead. 144 00:11:55,270 --> 00:11:57,280 ‫I can use the new keyword. 145 00:11:57,280 --> 00:12:03,880 ‫So what that will do is it will simply say, okay, this show details has priority over the car show 146 00:12:03,880 --> 00:12:04,660 ‫details. 147 00:12:05,430 --> 00:12:09,810 ‫So it's not overriding it, but it's reprioritizing it. 148 00:12:10,500 --> 00:12:11,700 ‫So now how do I shoot? 149 00:12:11,730 --> 00:12:15,130 ‫Show this show detail method or use this show method? 150 00:12:15,150 --> 00:12:26,430 ‫Well, let's create a new BMW pmwz3, which is going to be a new BMW car with 200 horsepower. 151 00:12:26,910 --> 00:12:27,990 ‫Let's make it black. 152 00:12:27,990 --> 00:12:31,440 ‫And then it's az3 model. 153 00:12:32,650 --> 00:12:36,130 ‫And then let's also create an Audi, Audi a three. 154 00:12:36,460 --> 00:12:42,490 ‫It's going to be a new Audi with, let's say, 100 horsepower. 155 00:12:42,490 --> 00:12:43,720 ‫And this one is green. 156 00:12:43,750 --> 00:12:46,270 ‫Very weird color for a car. 157 00:12:46,270 --> 00:12:49,300 ‫And then you have a three model here. 158 00:12:50,590 --> 00:12:56,920 ‫Now, let's run the BMW and show details method here. 159 00:12:56,920 --> 00:12:58,780 ‫And the same for the Audi. 160 00:12:59,800 --> 00:13:00,730 ‫Audi. 161 00:13:00,760 --> 00:13:03,520 ‫A three to show details. 162 00:13:04,890 --> 00:13:06,240 ‫So let's just run that. 163 00:13:09,610 --> 00:13:14,080 ‫And you see HP 200, color black HP 100 color green. 164 00:13:14,350 --> 00:13:21,390 ‫So as you see, it's calling the car show details method and it's not calling the one from BMW. 165 00:13:21,400 --> 00:13:26,840 ‫And that is because we have created it as a car, but it's a new BMW as a car. 166 00:13:26,860 --> 00:13:35,740 ‫So if we do the same thing now, let's go ahead and create a BMW MX five, which is going to be not 167 00:13:35,740 --> 00:13:48,280 ‫a car, but this time it's a B, M W new BMW and this one, let's say, has 330 horsepower. 168 00:13:48,550 --> 00:13:53,140 ‫It's white and it's an MX five. 169 00:13:53,590 --> 00:14:03,920 ‫So now if I call this B M W MX five dot show details method, it will show me the brand as well so that 170 00:14:03,940 --> 00:14:09,040 ‫we are brand BMW, HP 330 color white. 171 00:14:11,010 --> 00:14:12,060 ‫And the first ones. 172 00:14:12,060 --> 00:14:13,360 ‫So those ones here. 173 00:14:13,380 --> 00:14:18,870 ‫They also had to show detailed method, but they used the one of car and now the BMW one uses the one 174 00:14:18,870 --> 00:14:20,050 ‫of BMW. 175 00:14:20,070 --> 00:14:28,020 ‫So even though we said new BMW, we still use the car class in order to create a car object of of that 176 00:14:28,020 --> 00:14:36,480 ‫which is internally a BMW, but we call the cars methods and not the BMW methods only if we go ahead 177 00:14:36,480 --> 00:14:40,320 ‫and create it as a BMW we call the new method. 178 00:14:40,320 --> 00:14:47,340 ‫So this new here, it simply says, okay, if you create an object of me, then go ahead and write it 179 00:14:47,340 --> 00:14:47,950 ‫as new. 180 00:14:47,970 --> 00:14:54,750 ‫If you don't, then go ahead and simply use the method of cars or of the car class with the base class 181 00:14:54,750 --> 00:14:55,470 ‫pretty much. 182 00:14:56,400 --> 00:15:04,170 ‫But what if I want to call the show details method of my base class onto my BMW object? 183 00:15:04,440 --> 00:15:06,230 ‫Then I can do the following. 184 00:15:06,240 --> 00:15:18,720 ‫I can go ahead and create a car which I'm going to call car B, it is going to be a BMW m five. 185 00:15:19,350 --> 00:15:21,060 ‫But it's going to be. 186 00:15:22,530 --> 00:15:23,170 ‫A car. 187 00:15:23,190 --> 00:15:32,300 ‫So here I'm using casting so that I'm casting my BMW into a car and now I can go ahead and use the car 188 00:15:32,310 --> 00:15:39,090 ‫b dot show details of my BMW MX five. 189 00:15:40,990 --> 00:15:47,260 ‫Or of my car be so that we are once again hpp 330 colour white. 190 00:15:47,260 --> 00:15:51,220 ‫So that's our BMW m five here. 191 00:15:52,090 --> 00:15:58,900 ‫So why would you even use this approach of hiding and using this new keyword here onto your members? 192 00:15:58,900 --> 00:16:04,390 ‫Well, you would use that if you want your derived member to have the same name as a member in a base 193 00:16:04,390 --> 00:16:10,270 ‫class, but you do not want to it to participate in a virtual invocation. 194 00:16:10,480 --> 00:16:14,920 ‫So that's when you use this new keyword that we have seen in here. 195 00:16:15,040 --> 00:16:21,160 ‫Otherwise, if you want to override it, you can simply use this override approach here or this override 196 00:16:21,160 --> 00:16:24,520 ‫keyword in your declaration of the method. 197 00:16:25,660 --> 00:16:26,020 ‫All right. 198 00:16:26,020 --> 00:16:28,840 ‫So that's quite something or polymorphism. 199 00:16:28,840 --> 00:16:32,110 ‫In the next video, we are going to check out how we can prevent that. 200 00:16:32,110 --> 00:16:36,160 ‫So if you don't want that a method can ever be overridden. 201 00:16:36,160 --> 00:16:39,070 ‫There is a way to do that in our base class. 202 00:16:39,070 --> 00:16:40,690 ‫So see you in the next video.