Welcome back.
In this video we're going to look at polymorphism and look at another aspect that we haven't covered
of polymorphism yet.
We did cover one of them which is the override keyword and the virtual keyword, which allows us that
our deriving classes can override a method that was designed in the base class.
But there is another aspect to polymorphism, and we're going to look at it in this video.
So I am using this with classes.
Again, I'm not using interfaces at this point.
However, I want to use interfaces after that again to show you an alternative route.
Okay.
So we now have this public class animal with a virtual void Makesound.
So this is a method that our animal has, but we are allowed to override it.
That's why we're using the virtual keyword.
Then we have our dog animal and we're saying override the method Makesound of animal and use my own.
So I have my own implementation which says right line bark.
So the dog is going to bark and animal is just going to make some generic animal sound.
So then we also have a cat.
The cat also has its own implementation of makesound.
And the implementation of the cat is going to be meow because the cat is inheriting from animal.
So we're not using interfaces here, right?
We are using normal classes.
So animal is a normal class, which is why it also implements what this Makesound method does.
Now using or coming to the second aspect of polymorphism.
So as we said, the first aspect was that we are overriding the base classes methods implementation
with the within the deriving class.
So the parent class has a method and we're overriding it in the child class.
So now what we're going to do is we're going to use the second part of polymorphism, which is that
we are treating a dog like an animal, and we are treating a cat like an animal.
Even though a dog is a dog, it's also an animal.
So let me show you what I mean by this.
So we have this animal, let's say my dog equals new dog.
So what is going on here?
The thing is.
So this is part two of polymorphism okay.
This is a Greek word poly.
Whenever you see this poly you know it's a Greek word.
And also whenever you have ff pronounced as f, you also know it's a Greek word or it has a Greek origin.
So polymorphism we, we see that we create an animal object.
But then actually we're storing a dog object in it.
So what's going on here?
The thing is dog is inheriting from animal, which means that it has all the skills that an animal has.
So we can just use it interchangeably here, but we cannot do it the other way around.
So we cannot say dog.
My second dog is new animal.
That does not work.
It only works the other way around.
So it says cannot implicitly convert type interface app animal to interface app dog and explicit conversion
exists.
Are you missing a cast?
So we could cast it, but that's not something I want to go into right now.
I just want to show you that whenever a class is inheriting from another class, we can use it as we
see it here.
So what can we do now?
We can go and make the sound of the dog.
Let's see what this is going to give us.
And we will see that it is barking.
So why is the animal suddenly barking?
Because the animal is a dog.
Okay.
So by doing it this way, no matter what we named it, we could have called it my cat or whatever,
my, my something.
We could have called it anything we wanted.
That's not what matters.
It's what it comes here.
This new dog that's what does matter.
And what sound does it make?
Well, it makes the sound of the dog and not the sound of the animal.
So this is just a demonstration of polymorphism.
And I'd say that's it for this video.
See you in the next one where we're going to combine a lot more.
So we're going to combine polymorphism with interfaces.