Inheritance is one of those fundamental aspects of object oriented programming.
It means classes can inherit properties or functions from superclasses.
It means one class can become a base class for somebody else.
Now the good news is you have actually been using inheritance for the most part of everything we have
been doing.
But now in this lesson I'm actually going to make my own classes and then have a parent child relationship
between them.
That's how we can do that.
I'm going to go to school and start a new project and I'm going to call it inheritance science street.
So here is how what I want to do and how I'm going to do it I'm going to write it down first I'm going
to have a class of vehicles and then my class because I'm assuming this the vehicles that they go on
land essentially.
Now I'm going to say that has a subclass called sedans.
All right so I have the Waco's class and from there we have the seeding classes and then begin to learn
how to override functions.
And then we're going to learn how to add new functions and all that's pretty much most of for the really
matters.
So let's go ahead and get started.
I have a class called Veeck.
And it doesn't have a superclass.
What it does have is this it has a variable called tires and that's an integer.
Explicitly wrapped optional that it must be there.
So my vehicle has a number of tires.
How many tires are there.
And then I'm going to go ahead and say it has an in it in tires and there's going to be an integer because
you can imagine make self tires be calm in types.
I'm actually going to go ahead and add one more function of my own as that I'm going to call it.
Charlie vate.
Let's call it an integer input rate.
So essentially of the other reference I am writing in in an X and Objective-C so input rate which is
going to be an integer.
Right.
So I have that and the return is actually a boolean.
So whether this is going to be possible or not and I'm going to say by default the carry rate actually
returns false.
Unless our vehicle can carry something so let's see how we're going to subclassed these and how are
we going to inherit this stuff from it.
Let me clear things up so we can actually you know write more code in here.
So the next class I have is a class of sedans so I'm going to say class now when I'm defining my sedan
class I'm telling myself well every seat on is kind of a vehicle.
It has to have a certain number of tires and it can carry a certain amount of weight.
It might have an interest in it that initializes a certain number of tires.
So what if I say my sedan is actually a child of vehicles or you could read it in a way that CNN has
a base class called vehicle or vehicle is the superclass of Sedan.
So that's about that.
I have this now and I have a class called sedan and if I wanted to I could actually go ahead and use
it.
Let me again get myself a little bit more space and I'm going to use it for you.
I'm going to say let me bring this one down.
OK.
All right so now I'm going to go ahead and say let's call it what's a C Now let's say Ford Fusion for
the fusion.
That's a sin.
I hope so.
It's a sedan.
And the only type of it that it has is the input of number of tires they say well it has four tires.
So now my sedan class is essentially the same class as vehicle.
It's exactly the same thing.
But what have see now I have a variable called sunroof and that would be a boolean value.
And that's the specific to see whether they have sun roof or not.
And now as you can imagine in Ford Fusion I could go ahead and say some truth is for instance true.
This same wasn't valid for something that is made of the type we cause.
In theory I could go ahead in here and say my Ford Fusion is a vehicle with input tires for.
But then this one is going to give us an Aero is going to say so what is sunroof in here exactly.
Because a vehicle doesn't have a memory card sort of.
So so far we managed to have a class that borrows some of stuff from its parents class C Don borrows
from bacon and adds its own set of parameters and attributes.
Siddhant and also have its own in it.
So for instance I'm going to see on every seat on has a number of tires which is four.
So I'm going to go ahead and say it him Sundra of sun roof which is a boolean value and then you say
the body of my knee I'm gonna say the first thing is I have to initialize this.
Based on its superclass.
So I'm going to say super dots in it and say well what's the number of the tires.
And let's say the number of those is always four.
So whenever I see is being shallows that's going to be for and then I'm going to say self son who is
going to be in put some of.
So now we have a new unit for RCA Don that does initialize or object based on a sound off and we can
use that.
I'm going to go in here and say sit down in put some growth through.
Well I don't need that line anymore.
So what happened so far.
We have a base class the base class has its own initialiser.
It has a variable in there and we have a child's class or a sub class and subclass has a new property
or a new attribute.
I has a new in it.
Now here's another thing a very common situation that happens sometimes you want to overwrite an existing
in it.
So how do you do that.
I'm you're on say override in it and it says well you're trying to override it.
This one.
Yes that's the one I'm going to do.
And where are you going to go.
I'm going to say super DOD in it put the same value in them.
All right.
Or if you want manually pull put forward which kind of renders this one moot.
Now the next thing I'm going to do is I'm going to say Songdo is Fox so by default whenever I override
this in it and when I get the sunroof to be favs and that's overwriting and initialiser from our superclass.
Imagine if you're initialiser you have to do something different than you or our parents class and that's
something you override the initialiser you could of course overwrite an existing function.
I could go in here and say as you can imagine there is no order between the methods of just writing
in here so it's easier to zoom in to the screen and what to say overwrite function and the function
I want to override is this guy.
So not in this.
All right.
I'm going to say well it doesn't always return false if a simple rate is below 2000 than our return
through else let me put the else over there as well.
I'm trying to be more economical with my space illustrator and false of course.
So that's about you know overriding a function.
So that's pretty much the basics of inheritance.
You have a class that class has some stuff in it you subclass it to another one and that one can add
on a stuff overwrite functions from parent class override units from power Pyne's class have its own
specific in it such as this one and work essentially independently.
Something you might be wondering is so one why would we use this.
When do we use a situation like this.
Almost entirely always.
And that's the principle of object oriented programming trying to modularize your code.
So it is essentially easier to write and to read.
So if I'm writing an application let's forget about the cause and see what happens and what not.
Let's imagine I'm writing an application that say real estate listing application.
You have multiple different types of states right.
So for instance you have listings that are for sale you have listed that are for rental and then you
have listening that are residential and then you have these things that are commercial and then you
have less things that are in the city.
All right.
And then you have these things that are perhaps you know out of a state or whatever that you are trying
to do.
There are lot of different things that are common between all of these things.
So in a situation like this I would go ahead and say class property and then subclass sale and then
from subclass of state I would get something called residential.
Let's quickly demonstrate say that I'm going to say costis call it property and this is a class that
has the building blocks of a listing that you know you can have your app and then on that I have a class
and that class is going to be sale.
And that's a subclass of property property.
And then I'm going to have another class that's going to be residential and that superclass of that
is going to be saved.
So it's essentially a hierarchy of different objects that they inherit from each other.
Every one is a property.
Everyone is for sale.
So let's imagine everyone has something called price.
Right.
So price is an integer value for instance.
So everybody has a price.
And then the ones that are for sale.
They also have availability for instance.
So let's go ahead and say Vire Let's see.
And that's going to be a date right.
And then the ones that are residential are for instance they have something called variable number of
or just rooms.
All right which is a number of the rooms that are available you know residential for sale property so
on and so forth you know all of these things they essentially trickle down.
They go from one to another to another and that's you that's how your code becomes more and more modularized
and easier to read and easier to understand.
We will see a lot of these kind of things in the coming you know examples on the actual apps that we
make.
We try to subclass as much as we can.
Another thing that you know you often end up doing is subclassing something that already exists.
Phrase as I want to get a class called my a string to be a subclass of the string because I want to
add an extra function to it.
I want something to happen in here which Genesys thing doesn't do it by default.
That's all I'm working on an app that has to I don't know who to to reverse every listing that I give
it to.
I give to him and I want to have the built in feature of this thing so I can just going here on a new
class that inherits everything from a string but also has some extra stuff in it.
And we will see more of this when we talk about class categories and class methods as well.
For now if you have a class this is how you stop classes this is how you inherit from that one you are
good on the stuff you overwrite functions and you use them independently.
That's about that.
And let's move on to our next guest.