‫Welcome back to the polymorphism example.
‫In this video, I'm going to use a different approach where you first of all have a little challenge.
‫You should write some code which will help you to understand inheritance a bit further or a bit better.
‫And then we will go into the poly forms, make part of this whole program.
‫So please go ahead and try to write the code according to this challenge here.
‫So pause the video and try.
‫All right.
‫I hope you tried it.
‫So I'm going to create a new class ed class, and I'm going to call it car.
‫And this class is just going to be a general car class.
‫And it can contain or it should contain properties.
‫Right.
‫As I said, it should have a property called HP Prop and it should be an integer and HP is going to
‫be the horsepower.
‫So how powerful is this car?
‫And then another property which is of type string and which is the color?
‫Now what I need next is to have a constructor.
‫So I'm going to create a public car which takes in the HP and the color.
‫And here I just set this dot HP as the HP and this dot color as the color.
‫So this color is going to be our property color and the HP is going to be the HP of our class or of
‫our object that is created off type car.
‫Now the next one should be the show details method.
‫So public void show details.
‫And in here we just write something onto the console.
‫So CW, HP is going to be plus HP.
‫Then we have the color, which should be something like the color.
‫So of course I could use a different approach where I use string manipulation here, but I'm just going
‫to keep it like this.
‫Very simple.
‫So the next one is a public void and it's called repair.
‫And we're going to change those two methods later on.
‫So no worries.
‫But for now, I wanted to keep them very simple.
‫With the approaches that we have used so far, that will make it more clear what polymorphism does and
‫here just says car was repaired.
‫All right, so that's our car and that's our base class.
‫And now we need the two other classes, which is our BMW, BMW class.
‫And in here, as I said, we have an additional property called model and this is a string model.
‫Now, another aspect was the brand, which is a private member.
‫So private string brand and it's called BMW.
‫Now next we need a constructor, so a public BMW.
‫And before I do that, before I create the constructor, I want to inherit from car.
‫So now my BMW is going to inherit from car.
‫And what I need here is the HPP, then the color
‫and the model and here and comma.
‫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
‫color.
‫It's going to be the color and the model which is our property should be the model.
‫So this one here, there, I use this because I want it for this object and the same thing I can do
‫here and I can do here.
‫So even though I don't have created those in here, as you see, I can still use them.
‫Now, here's something that you haven't seen yet, which is base.
‫So I can use the base keyword here and then I can simply say, okay, I want to use the HPP and the
‫color of my base class and then I don't need those two lines anymore so I can even get rid of those.
‫As you can see now, the error disappears as well.
‫So now we have created a constructor which uses the base constructor of our base, which means of our
‫base class, which is our car.
‫It's just going to use this constructor and it's just going to add the model.
‫So now we have a very short constructor which does all the job, all the whole job.
‫So now what I need next is a void show details, as I said.
‫And in here I just want to show the details and I'm just going to copy the method from here or the entry
‫of the method.
‫And in here it's going to be the brand first.
‫So brand plus brand plus HP.
‫So now it's just going to show some additional details.
‫And here, as you see, show details, hides, inherited member car show details, use the new keyword
‫if hiding was intended.
‫And that's something that we're going to cover in the next video.
‫But for now, let's also create the repair method, public void repair.
‫And in here it should just say something like the BMW
‫and in here was repaired.
‫So what is this zero that I have there?
‫Well, that's the model.
‫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
‫or MX three or MX five was repaired.
‫So that's what this should do.
‫And now I need another one, another class, which is pretty similar to what we have here.
‫So I'm going to create a new class and I'm going to call it Audi.
‫And for simplicity, I'm just going to copy most of the stuff here, actually, the whole class, because
‫it's going to be very similar.
‫What is different is here that it's an Audi and that we have the Audi as the constructor and of course
‫it needs to inherit from car.
‫So now that is done and now comes the polymorphism part.
‫So we have an Audi which is of type car, we have a BMW which is also of type car.
‫And we have this base class which is car, and now we can go ahead and create multiple objects of that.
‫Now it will make a lot more sense as soon as we get there.
‫So let's go ahead and create a new variable called cars, which is going to be a new list of cars.
‫And even though this is a list of cars, so of the car class, we can create all these and BMW is in
‫here.
‫So just can create, let's say an Audi which is blue and is an A four.
‫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.
‫So that's one part of polymorphism that you can use the subclasses instead of the base class or the
‫deriving classes instead of the base class when working, for example, with the lists or in general
‫and by general, you will see what I mean.
‫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
‫my cars in my cars list.
‫And it's just going to write something like car repair or it's going to call a repair method.
‫All right, so let's call that or let's run that.
‫But before we do that, we need to console the right key.
‫Because otherwise our program will simply close.
‫So there you are.
‫Car was repaired.
‫So now it's calling the car method here.
‫Which which is this one here?
‫Car was repaired.
‫So it's not calling this repair method and neither does it call this repair method, which was the BMW
‫was repaired or here it's the actually here it should be audio or Audi.
‫Audi was repaired.
‫So now why does it call the repair method of our car and not the one of BMW and and the Audi one that
‫is has to do with two things.
‫So one of them is we have this void repair, which is not a virtual one.
‫So if we make it virtual void repair, what we now can do is, for example, here we can override it.
‫So now we can override the repair method.
‫What that will do is if we run the code again.
‫We will see.
‫Car was repaired.
‫That's our Audi, the first one.
‫And then the BMW M three was repaired and that is the second one.
‫And that is because our BMW class here has overridden the repair method.
‫So we can simply take this method, make it virtual, and that allows us to override it in the deriving
‫classes.
‫And that's what we have done here.
‫So now there are the two aspects of polymorphism.
‫So polymorphism network one and ODI and VW, Porsche, whatever different classes you want to have can
‫all be used wherever a car is expected.
‫So whenever they are inheriting from this class here, from our car class here, this one, we can simply
‫use it instead.
‫And then the second aspect of polymorphism is that the virtual method can be invoked on each of the
‫deriving classes, not on the base class.
‫So that's something that we have done with the BMW and now we can do the same thing with our order here.
‫So I'm just going to make that an override as well.
‫Override, void, repair.
‫Now let's run it again.
‫And now both should show their brands and their models.
‫So we see the Audi A4 and the BMW M3 were repaired.
‫There is one more thing about polymorphism, though.
‫So if you want to hide the base class members with new members, you can use the new keyword.
‫So let's say we have this void show details method in our class car, which is just a void.
‫It's not a virtual void.
‫And now if I would like to override that, so not override in the sense that we have done here with
‫the override one.
‫But I actually want to use this method instead whenever I'm using a BMW and not in the sense of overriding
‫the old one, but just hiding the one from car and using show details instead.
‫I can use the new keyword.
‫So what that will do is it will simply say, okay, this show details has priority over the car show
‫details.
‫So it's not overriding it, but it's reprioritizing it.
‫So now how do I shoot?
‫Show this show detail method or use this show method?
‫Well, let's create a new BMW pmwz3, which is going to be a new BMW car with 200 horsepower.
‫Let's make it black.
‫And then it's az3 model.
‫And then let's also create an Audi, Audi a three.
‫It's going to be a new Audi with, let's say, 100 horsepower.
‫And this one is green.
‫Very weird color for a car.
‫And then you have a three model here.
‫Now, let's run the BMW and show details method here.
‫And the same for the Audi.
‫Audi.
‫A three to show details.
‫So let's just run that.
‫And you see HP 200, color black HP 100 color green.
‫So as you see, it's calling the car show details method and it's not calling the one from BMW.
‫And that is because we have created it as a car, but it's a new BMW as a car.
‫So if we do the same thing now, let's go ahead and create a BMW MX five, which is going to be not
‫a car, but this time it's a B, M W new BMW and this one, let's say, has 330 horsepower.
‫It's white and it's an MX five.
‫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
‫we are brand BMW, HP 330 color white.
‫And the first ones.
‫So those ones here.
‫They also had to show detailed method, but they used the one of car and now the BMW one uses the one
‫of BMW.
‫So even though we said new BMW, we still use the car class in order to create a car object of of that
‫which is internally a BMW, but we call the cars methods and not the BMW methods only if we go ahead
‫and create it as a BMW we call the new method.
‫So this new here, it simply says, okay, if you create an object of me, then go ahead and write it
‫as new.
‫If you don't, then go ahead and simply use the method of cars or of the car class with the base class
‫pretty much.
‫But what if I want to call the show details method of my base class onto my BMW object?
‫Then I can do the following.
‫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.
‫But it's going to be.
‫A car.
‫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
‫b dot show details of my BMW MX five.
‫Or of my car be so that we are once again hpp 330 colour white.
‫So that's our BMW m five here.
‫So why would you even use this approach of hiding and using this new keyword here onto your members?
‫Well, you would use that if you want your derived member to have the same name as a member in a base
‫class, but you do not want to it to participate in a virtual invocation.
‫So that's when you use this new keyword that we have seen in here.
‫Otherwise, if you want to override it, you can simply use this override approach here or this override
‫keyword in your declaration of the method.
‫All right.
‫So that's quite something or polymorphism.
‫In the next video, we are going to check out how we can prevent that.
‫So if you don't want that a method can ever be overridden.
‫There is a way to do that in our base class.
‫So see you in the next video.