Hey what's up everyone.
Mark Price you're a dove 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 their Swetnam bleeding and you know you're out there and you get an inheritance and
you're just like 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 inherited 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 to view class and a vehicle class has some basic features list.
We'll 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 a 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 car's not moving.
And so may be 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 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 two.
OK.
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 a sports car.
Just for fun sports car inherits from vehicle 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 an a model.
What we need to do here actually is create an initialiser first like so.
All right and then and here we can set our make and our model like so is yelling at us because it wants
us to override it.
So we need to call super in it super 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 it right here 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 X code doesn't
crash on us.
It crashed.
So OK.
But I want to do is create an instance of the sports car so let car equals a new sports car.
And what's interesting.
Fit aside soloed 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 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 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 acle we could have done the
exact same thing there.
And then you know initialiser or let's let's just copy what we got here.
Let's make it real 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 no excuse me current speed plus equal speed increase.
You know without multiplying it by no 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 it could easily have created a parent class which
creates a deeper 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 Aiwass development so.
Mark Price your DENSELOW dot com.
Movin on and.