A what's up everyone.
Mark Price you're a dub slopes dot com.
And today we're going to talk about inheritance not like the inheritance you got from your grandfather.
You know when all your peers were out there working their butts off you know failing and failing and
failing again and they're sweating and bleeding and you know you're out there and you get an inheritance
and you're just like hey here's my Bugatti.
You guys suck.
Yes not one of those kind of things.
It's a little bit different.
It's more so like an inheritance of features more like I have red hair because my grandfather has red
hair.
You've been Herrod some type of features from some type of relative.
So I'm going to get started with the playground and we're going to call this in heritance inheritance.
It's one of those words that's easy to misspell.
Inheritance is a principle of object oriented programming.
OK.
Object oriented programming.
So a perfect example is continuing on with a car or a vehicle.
We've got a view class OK and a vehicle class has some basic features less we'll will go ahead and say
like var tires equals four.
You've seen something like this before and then bar you know make of type string.
But right now there's nothing in it bar model of type string.
OK.
And we said phunk drive and then we can say phunk for aik OK and save our current speed of type let's
say double.
And we started off at zero.
The cars in moving.
And so maybe OK let's say let's say you are creating can complex drive algorithm where it's going to
move accelerate the car forward.
So we pass in the speed increase K of type double.
And then maybe the base.
So for a basic car the basic algorithm is this it is this current speed.
OK.
Plus equals speed increase times.
Let's say two let's say that was the algorithm some random arbitrary algorithm the current speed of
the vehicle equals the current speed plus speed increase times 2.
Now this going and create what's called a subclass.
OK so this is a class let's create a subclass.
We're going to show inheritance here.
So a class
what do I want to say.
Class BMW.
OK class BMW or even better.
How about sports car.
Just for fun sports car inherits from vehicles so you start inheritance by putting a colon here and
then specifying the type K which is this this is the type the type or class that you want to inherit
from.
Now this is really cool there's nothing in here right.
Nothing at all.
But you can still access the properties of the parent class so on this sports car you know tires are
going to stay the same but you know maybe we can set the make and model so I don't have to recreate
those which is really cool I can say make equals and let's say BMW and model equals the say three series.
OK.
So we've got to make it a model.
What we need to do here actually is create an initialiser first like so.
All right.
And then in here we can set our make and our model like so it's yelling at us because it wants us to
override it.
Yeah.
So we need to call super and it super and it just some boilerplate stuff you've got to do in every class.
OK.
So every single class has by default an initialization function but you don't see or hear because we
did not implement it.
So let's implement it now on the parent class.
OK.
So we've got to initialize initialiser on the parent class.
And initialiser here in the subclass.
And this is interesting.
We're calling super init on the parent class.
What does that even mean.
Well let's print this out.
I am the parent.
OK.
And then just print this down here.
I am the child and what I want to do is just create an instance of this class penning the code doesn't
crash on us.
It crashed.
So OK what I want to do is create an instance of the sports car so let car equals a new sports car.
And what's interesting in the if you decide to load I am the parent Prince and I am the child of what's
happening here.
So we create the sports car and then this initialiser is called and the override function says I want
to override the parents initialiser function.
So it won't be called at all.
But then we actually call it here explicitly.
So then that's going to go back up to the parent class and call was whatever in it.
So sometimes parent classes have set up or things like that and introduce We want to call it.
It's typical to call the parent class and then to create your own functionality which is really cool.
OK so here's our sports car.
Awesome.
Now let's air our sports car needs to drive faster than the standard algorithm.
So what we can do is we can even override the functions so I can type and drive.
That's going to override it.
And now what I could do instead of current speed being equal to this I can say current speed plus equals
speed increase times three.
So this one is times too but this was times three.
And now the child's class we know that every car needs to have a drive function.
And so that's great.
Like why would we we wouldn't want to create a bunch of different classes and re-implement this function
every single time.
So it's already pre-built and we just wrote it once and we overwrote it here which is great.
Every vehicle needs to be able to drive but its implementation could be different purview.
Is a Ford F-150 going to accelerate differently than a Ford Mustang.
Yes every car is going to have its acceleration.
Therefore it would have its own code here in the functions that it's overriding.
And of course you can override the properties as well too.
So the whole point of this lesson we could go way down this rabbit hole where we're not going to.
The whole point of this lesson to know is that you can create a parent class and children classes can
inherit from that parent class and you can do more than one.
So if we bought a sports car we could say class truck inherits from view school we could have done the
exact same thing there.
And then you know initialiser or let's let's just copy what we've got here is make it really easy so
it doesn't yell at us OK.
And you know we can get rid of these here.
OK and then a trunk a truck could have its own drive function that you know speed increase you know
plus equals or excuse me current speed plus equal speed increase.
You know without multiplying it by a multiplier.
And so anyway now we've got two different classes here that both inherit from Buca they both have a
drive function but their implementation is different and why this is important is because it helps you
create objects that can adapt to different situations.
For instance you know on Instagram you may have a parent class that has you know a specific base filter
but then you may have different filters like Valencia and other things that have their own algorithms
that they need to implement.
So rather than putting it all one giant class file they could easily have created a parent class which
creates a deep filter and then they can override it and add some other functionality to it for those
specific filters.
Again all kinds of things you can do with classes objects and inheritance but that's all you need to
know now you're going to learn about it more as you use it in Iowa development.
So Mark Price your DENSELOW dot com move movin on an ad for.