Welcome back.
In this video we're going to look at method overriding.
And in order to use overriding we need to use the override keyword and the virtual keyword.
Now why would we even do that?
The thing is we if we look at our example, we have our barking dog and our meowing cat.
Okay.
But actually every animal does make a sound, at least some sound, right?
Or any way of communication.
So a dog might bark, a cat might meow, a bird might chirp, you know, like every animal has.
Well, maybe there are some animals that don't have organs that generate noise or whatever or make a
sound.
But let's just assume every animal has a way of making sound.
So let's go ahead and create a method for that public void Makesound.
And now here we're just going to make a generic sound.
So animal makes a generic sound.
So what I want to do now is I want to implement make sound to be unique for the dog as well as unique
for the cat.
So very specific, so to speak.
In order to achieve that, what I can do is I can actually let me override or change the name of the
bark method and the meow method to be make sound.
However, this is not yet correct.
So we're still missing two keywords that I was talking about, right?
The virtual keyword as well as the override keyword.
Now you can even see when you hover over it, it says it hides inherited member animal make sound.
Use the new keyword if hiding was intended.
Well, that's not what we want to do.
What we actually want to do is we want to override.
Now, in order to achieve that, what we need to do is we need to add the keyword virtual here.
So this is the virtual keyword which now allows us to override the make sound method in deriving classes.
So now we can say public override void make sound.
So you see the warning goes away.
And I'm going to add the override keyword to my make sound of the cat as well.
So the cat is another deriving class of animal.
And now we're going to have unique make sound methods for each of the animal types, at least in our
case.
So let's say we have my dog and now we can make it sound.
Now let's do the same thing with the cat.
So Cat, my cat is going to be a new cat.
And now let's use my cat, my sound.
By the way, I'm going to get rid of this eat method.
And now let's run this again and we will see barking and cat is me.
Okay I cut off the text here meowing.
Now let's run this again and you will see the cat is meowing and the dog is barking.
Woof woof.
Okay, so that's what the override keyword and the virtual keyword allows you to do.
Now why would this be useful?
Well, the thing is, sometimes you just really want to use the same method name with a derived class
because you want to do something similar or its own implementation, so to speak, for each given class.
And in order to achieve that, you need to override it.
And in order to make a method overrideable, you need to make the method in the base class virtual.
So this virtual keyword now allows us to override the method.
You see, if I get rid of this virtual keyword, I get an error here.
It says Makesound cannot override inherited member animal make sound because it is not marked virtual
abstract or override.
So that's why we're adding this virtual keyword here.
And then we're saying, you know what?
Make sound can be overridden.
However, how do I now access this base method of my animal for a dog and a cat.
And that's something that we're going to look at in the next video.
Before we do that though, I want to make sure that you understand that what we just did with this overriding
and this virtual keyword is part of polymorphism, which is the third part of object oriented programming.
But there is a lot more to know about polymorphism, and we're going to cover that later on in the course.
For now, I just want to go over the other points of inheritance, but then we're going to really dive
deep into polymorphism as well as the abstract keyword.
So yeah, let's look at the base keyword in the next video first.