Welcome back again we're going to conclude our brief introduction into object oriented programming by
topic but a blurb.
By talking about polymorphism.
OK.
And man sometimes just the words I want to come out and they don't come out.
But they came out supinely morphism that's a hard word and that's hard enough for it to say as it is
and it's a question that's often asked in interviews for jobs which is funny.
There was like can you please define polymorphism and you're like oh what do I say.
Because the definition so hard the implementation of it is so easy.
But the definition is pretty hard.
So we're going to say define polymorphism define Pawley morphism
the condition of occurring in several forms.
The occurrence of different forms among the members of a population or colony or in the lifecycle of
an individual organism.
So it Socan this is just generic definitions right.
Talking about having different forms occurring in several several different forms.
Interesting right occurring in several different forms.
Here's another one.
Some Please find it in two sentences.
Polymorphism allows the expression of some sort of contract with potentially many types.
Implementing that contract whether through class inheritance or not in different ways each according
to their own purpose coding code using that contract should not have to care about which implementation
is involved only that the contract that the contract will be obeyed.
I like that one a lot and this is a programming definition.
So you're going to create a contract and then different things can implement that contract.
But they do it in their own way and you're probably super confused right now.
And you should be because that's the textbook definition that's compute.
That's what they teach you in college.
All right let's see if we can confuse our students and not teach them anything practical but teach them
things that don't make any sense.
So let's teach you whether or what they're all about get started with the playground here and we're
going to call this one.
Paul Lee more fearsome.
OK put it here in swithe.
That's great.
The principle is so simple once you watch me it is going to be like that.
And that's how you should lead in your interview questions by talking about.
Well let me just give you an example instead.
So what we're going to do here is we're going to create a class called Shape.
OK.
And we're going to a property in it called Area OK bar area of type double.
It may or may not have a value and this will put an optional at the end of it and create a function
called calque relate area to.
OK.
Now here's our base class and every shape is going to have a calculate area function.
Ok so here's the advantage of working with a base class.
For instance let's say we didn't do this and what I wanted to get rectangles area so I would say class
and then I would have to go and see a rectangle.
OK.
And then I'd say our area of type double.
And then I would say phunk calculate area.
OK.
And then area would be.
And then we need to pass and to lengthen it with here right.
So laying type double and width of type double.
And then we would say area equals length times with.
OK.
And then all of a sudden the boss was like oh no we need to also create a triangle so we're like OK.
Like triangle or like var area double and then we're like phunk calculate area.
And then we get a base of type of all.
And what is a base and height divided by two.
I think so.
So we get the height which is a double and then so we say you know area equals based times height divided
by two and one just puts in print this is peroration.
OK so we've got two classes both doing similar things.
But look at this.
We had to recreate the area property we had to recreate this property here.
And you know what happens if in this case one programmer gets lazy and they're like calque area and
this one is just like you know let's get area.
So now things are becoming inconsistent and the parameters start changing.
You know base value.
And this is not really good.
We're rewriting code.
We can break our logic.
This is all trash trash trust trash.
Get rid of it you're fired.
Can't work here anymore.
I'm just kidding.
I'd still hire you.
OK.
So how do we fix this problem.
How to make things more consistent where we can use polymorphism and what we're going to do now instead
is we're going to create a new class called triangle of course.
But this time we're going to inherit from Shape OK inherit from shape and all we have to do is obey
the contract which is calculate area.
So what we can do is we can say I want to implement calculate area function and we override it completely
and then all we have to do.
Actually this is wrong.
This needs to have the values in it so let's put a value of type double and value be of type double.
OK.
Now we can calculate the area like so and then all we need to do is say area equals valet or times Balbi
like so divided by two and this is this and this is this and this is a try.
And in this case over here we want a rectangle class Rectangle.
And that's going to inherit from shape and we do the same thing here calculate area.
But in this case area equals value a times vel B.
So that's interesting.
We've got two different shapes here.
Implementing the exact same function but the logic inside is different and that is polymorphism.
OK.
This is one form ok with or this is one object or form of an object but that is taking different forms
like so.
OK so we were obeying the contract like we saw in the really great stack overflow definition our obeying
the contract and both these cases but they're both doing something different.
And we're still using the same parent class.
And that's what polymorphism and you use it all the time.
So next when you go in your job interview if you can't read the definition you're like well let me just
give you an example.
So when he says give me the definition of polymorphous and said Let me give you an example.
Imagine that you have a shape class and you have to calculate the area of shape but you don't know which
shape is going to be passed in at runtime.
So food critic calculate area function and then we also create two more classes like a triangle or rectangle
that inherit from shape.
And then at runtime they can each perform their own area calculation and assign it into the area value.
And so it doesn't have to know beforehand what type of shape you're picking because you have the different
implementation for it.
Or you could say something like that and then be like oh that's a really good example.
You know the same thing you know if you're building a videogame you know you might have a you might
have a humanoid class and a humanoid class is just you know a base parent class and then the humanoid
class might have Orcs because they're humanoid they got hands and feet they can speak to the Elves but
you know elves are magical and mystical they can do their own things and orcs are brutish and you know
ugly.
So they've got to put an ugly modifier on there.
And that's actually real that's a real example in games like World of Warcraft and other things.
They're using polymorphism and inheritance all over the place.
So that is polymorphism simple principle to understand once you do it and you're going to do it that's
it.
Mark Bryce had slopes dot com polymorphisms.