Hi, everybody, and welcome.
I'm super excited to talk about inheritance in this picture, but before proceeding to explaining it,
let's go over the topics that we have already covered.
OK, so we have already covered lots of topics in this course, and we've been through a lot of difficulties
and lots of explanations and logics and assignments.
So we've been through variables, collections, if statements, loop's methods, classes and objects.
And you have seen in the previous lectures that all of these topics come integrated in object oriented
programming in Python and you would need to dominate all of them in order to use OPIS.
I'm sure that it wasn't easy for you to understand all the topics and to practice a lot and to loosen
long hours of lectures.
But I'm sure if you did now, you can surely say that you are professionals in Python programming.
We're going to dive deeper into object oriented programming in this lecture and we're going to discuss
a sub topic of OAP, which is called Inheritance.
Inheritance is basically a behavior which is inside OPIS.
It can be used not only in Python but also in other OPIS enhanced languages such as Java.
Basically, inheritance is allowed.
It allows us to have a parent child relationship between classes.
We're going to create two classes and one of them would be defined as the parent and the other one would
be defined as the child.
In all cases, in computers, not only programmers.
In programming, a parent is something that gives something to the child.
It's above the child.
And also in this case, that relationship that we're going to have between these two classes that we're
going to create is the following.
But first, let's talk about real life definition before we dive into coding.
Let's assume that we have a broad.
Category in all war in our world, this, we can call it a vehicle, OK, a vehicle is not something
specific.
A vehicle can be basically several stuff, several things.
It can be a private car, a truck, a bus.
OK, so if a vehicle is like a broad name for lots of things.
So in our lecture, we're going to create a class with the name vehicle.
OK, and you can already understand that vehicle would be the parent class.
OK, so we're going to have two classes in the same Python file, first one at the top.
This is going to be a vehicle.
Right?
And then beneath it, we're going to create a separate class, which will be a child class, in our
case, private car.
So we're going to have these two classes.
Right.
And why do we have in common between them?
OK, so in vehicle, we're going to defined two things.
But before talking about these two things, let's talk about common ground between all vehicles.
Can you agree with me that all vehicles have wheels, doors, steering wheel, fuel tank and other items
as well?
Right.
So we're going to define one method and one property in the vehicle class, and then we're going to
use an instance of an object of a child class.
And this child class is going to use this method and a property that we declared in the vehicle.
OK, so the property in the method would be declared here.
But we're going to create a separate class private car and using instance of this private car.
Are you going?
We're going to use this method in property which we created inside the vehicle, which is the parent.
So you have an certain inheritance here.
We're using property and methods inside the vehicle, but we're using a private car to initiate them.
OK, so basically all child classes would absorb all the methods and properties of the parent class.
OK.
Hold it for me for one second of this sentence, OK?
Child classes would absorb the methods and properties of the parent class and this is what we're going
to do now.
OK, so before talking how to do it, we're just going to see this example right here.
Right.
And let's go deeply and talk about all the details we see here, as always, making you like a monstrous
example like this.
But then once we break it down into small details, it's not so scary.
Right.
So let's start from the beginning.
Let's start from the outer structure.
OK, so what you see here is going to appear in one python file, right?
In one python module you see here, class vehicle.
And then you have the Init method and you have engine status method.
Already familiar with this box.
Right.
Is the same thing as we covered lots of times.
And here in the bottom part you can see an additional class.
The name is Class Private Car.
Right.
And inside here you can also see def in it and that's it.
OK, that's in it and that's it.
We're going to discuss these parts here in a second.
OK, but in overall, you can see here a class init method.
And down here at the bottom you can see instance of an object that belongs to private car class.
And you can see here, initiation or execution off engine status method.
OK, so you have class parents, you have additional class child, you have instance of an object,
an execution of a method.
Now, let's go into details.
OK, so up on here, you can see class private car.
And inside these round brackets, you can see I mentioned here the war.
The vehicle vehicle is the name of this class right here.
Right.
Going by the notes here at the right, you can see inherits all methods of vehicle class.
So once I'm having this class right here and I'm mentioning a name of a different class, basically
mentioning the name of the parent class.
Then once I'm creating an instance of an object which belongs to the child private car, I can use the
engine status method.
Which is up in here.
By this instance, so without this vehicle words right here, this is not possible because engine status
method belongs to this part, to this class right here.
So how come I have an instance belonging to private car and I'm using the method that belongs to a different
class simply by using the vehicle, which is the parent class inside these wrong brackets.
So this is inheritance of methods.
Simple as that.
Not once we covered the inheritance of methods.
Let's cover the inheritance of properties.
OK, it's going to be a bit more complicated, but follow me closely and I'm sure that you will be just
fine and we're going to understand it quickly.
OK, so going inside this definite method of the child right here, you can see here, first of all,
self that is engine running equals engine is engine running.
This is very simple.
We already familiar with the first car, with the first property right here.
But then you can see here Super Brackett's that you need.
And then is the engine running OK?
This line right here is a bit weird.
We haven't seen it before.
And this basically means is that we're having this instance here, right?
We passed a true boolean variable into it.
You see the number one here.
It's received up here inside the declaration, and then from here, it went into this property, which
is number three, and also went to this number three as well.
And this super Nenita phrase allows us to pass the variable from here up to the top to here and then
inside here and then inside engine status.
So by using this super unit phrase, once we execute engine status, OK, once we execute this method
and remember, we're using instance of an object of private car, it goes up in here, but it asks for
a variable self is engine running.
Is it true or is it false?
It asks us.
But the variable was passed to private car to this part right here.
And we need somehow from this part to pass it to this part to the parent.
And this is possible only by super any trace, so by using the super elite phrase, once we execute
a method that belongs to the parent, by using the super eenie phrase, we're also using the variable.
Through.
Which is pass from here to here to here, then up to the end of the parent and then goes inside the
method once it's executed.
All right.
This is not very complicated.
Let's go over it again, literally.
All right.
So first of all, private car class inherits method and a property from vehicle class.
OK, we understand that private car, for instance, this for focus equals private car inherits one
method and one property from vehicle, which is the parent.
Private car is the child while the vehicle is the parent.
By mentioning vehicle in the declaration of a private car class, we achieve that.
All methods from vehicle can be used by instance of private car by writing vehicle here.
We can use Ingeus engine status method, which belongs to the top class, but by riding the vehicle
keywords here, we can use all vehicles methods with this instance, which belongs to private car.
This regards to methods.
Now let's talk about the parameters, their property.
In the parent class, we use a variable that is called is engine running, OK, you can see is engine
running here, here and then inside.
The method here is engine running.
All right, in order to pass the property from private car class, we need we use a super unit phrase,
OK, so we have this method right here.
This method uses engine running, but it belongs to vehicle class.
Here we have an instance of private car.
So we insert a here variable, but we need somehow from this in here to pass it to the parent.
And we're we're achieving this by super unit phrase.
So we pass from here to here and into the method and it gets through here once we once we execute the
engine status.
OK, so in this example, I'm going to show you as well a live car example with the same code.
All right.
So you can see here the upper part class vehicle, the middle part class private car, and the last
part, which is the instance of an object, then also the running of this metal right here.
So first of all, I said that vehicle keyword is making is giving us the ability to use this instance
of an object from here.
It allows us to use engine status, which belongs to the upper class.
So let's test it.
Let me remove this keyword and see what happens.
You see, once they remove this keyword immediately, you can see it back around here.
And once they hover above it, it says unresolved attribute reference engine status for class, private
car.
It basically says to me, hey, do you have an instance of private car?
Why the hell are you using a method that belongs to a different class?
I'm not familiar with it.
With that method.
Not familiar with it.
What is it?
What is engine status?
It's not part of private car.
All right, but once I bring back the vehicle.
OK.
Once I bring back the vehicle into the wrong brackets, everything's fine.
All right, so this is regarding the method.
Now let's talk about the Parmeter, OK?
You can see the super here, the freeze now let me try to remove it.
OK, let's say we don't need it.
And once we initiated this method, everything's fine.
The true variable goes from here and goes automatically to the top into here, right into your status.
Let me just try to remove this in it.
And immediately you can see brownish background on the inside of the child here once hovering above
it.
Let's see what it says.
Call to end it off.
Superclass is missed, OK?
He's telling me.
He's telling me you're trying to get inheritance here, OK?
You're trying to get inheritance here, but what about the super phrase, OK, I can't use inheritance
without the super phrase.
And once we have over here, everything's fine.
OK, so this variable goes from here to here than to here and here.
And then from this line, it goes up to here and then here.
Right.
And then when I run engine status.
OK, this method I'm going to use is true, right, so it's already past up to here, so it's OK, it's
going to get the value.
Let's try to run it.
OK, so I'll have it through here then.
Engine is running then.
If I put false here, I'm going to get engineers off.
Right.
And then once I'm going to put A, B, C here.
So wrong value inserted.
It's going to replace the deflection point.
I think this covers perfectly the inheritance topic.
You can always you can also find this block of code attached to the lecture.
And I'm highly recommend you to copy it into a pie chart and to do some experiments, because my best
learning experience was by making making experiments on other people's code.
Thank you for listening and I will see you the next video.