Hello guys.
So we are going to continue our discussion with respect to Python.
And in this video we are going to discuss about inheritance in Python.
Now this inheritance is a fundamental concept in object oriented programming that allows a class to
inherit attributes and methods from another class, right.
So in this specific video I will be talking about how you can implement inheritance.
With the help of Python.
We'll be discussing about single inheritance and multiple inheritance.
And I'll be showing you multiple examples.
So let's go ahead and let's see.
Now inheritance is basically derived from the real world scenario.
Guys I hope uh, you know, like let's say uh, you will be inheriting your parents property.
You know, it may be house, it may be land, you know, but the vice versa will not be possible.
You know, you your parent will basically not be inheriting your property, but instead it will be going
to your children.
Right?
So this kind of scenario.
So this is based on the real world scenario.
And similarly the inheritance property in Python also works in the same way.
Okay.
So let me just go ahead and show you one specific example of inheritance.
And then I will show you how you can probably create a child class and probably inherit the parent class
altogether.
Right.
So I will first of all start defining my parent class.
Okay.
Now here, let me just go ahead and define one class which is called as car.
Okay.
Now car is my class okay.
So I can also go ahead and write like this okay.
Class car.
Now first of all, as you all know, whenever we define a class we need to probably create a constructor.
So let's go ahead and create this underscore underscore init underscore underscore.
And here I'm going to use one parameter that is self.
And uh the other parameters that we are going to provide while we are instantiating the object of the
car class, you know, like windows, doors, right, engine type.
So let's consider that these are some of the attributes that we are going to pass.
And let's go ahead and create some of the instance variable.
So I will go ahead and write self dot.
And the first thing is that windows right I will go ahead and create the first attribute that is windows.
And I'll assign this value to the windows that I'm passing over here in this init method.
Okay.
Then we will go ahead and write self dot doors okay.
Where I will be passing the doors value.
And finally I will go ahead and write self dot engine type okay engine type.
And again I've already told you it need not be this name need not be same as this.
You can write anything over here but these are the attributes or instance variable right.
So is equal to.
We will go ahead and write engine type okay.
Now this is my car class.
And this is what I have in terms of attributes and all.
Let me just go ahead and define one instance method.
Also let's say with the help of class car we usually drive.
So here what I'm actually going to do is that I'll go ahead and use self keyword okay.
And let me just go ahead and print okay.
And let me say that uh, the the car, the person will drive the car.
The person will drive the which car?
Okay, so let's say that the person will drive the petrol car or engine type car, right.
So here I will just go ahead and write engine type okay.
And here I'm actually going to use this f string okay.
Engine type.
Now engine type I cannot directly use it over here because I have to use self dot right.
Because engine type is an attribute over here, right?
So the person will drive the engine type car.
Okay.
And I have to probably if I want to use this attribute over here I have to use self dot okay.
So inside this this is my parent class okay.
I've defined a class with an init method.
The attributes are windows doors and engine type and one instance method which is called as drive.
Uh, and here I'm saying that hey, the person will drive whether it can be a petrol car or diesel car
based on the values that we provide right now.
Once I probably go ahead and create this class, let's consider this is my parent class.
Now I can go ahead and write car one is equal to.
I will go ahead and define my car okay.
And here I'm going to give the values.
Let's say number of windows are four.
Number of doors are five.
And let's consider the engine type is petrol okay.
Petrol Petrol car okay.
And now I will go ahead and print car one dot.
The method that is drive right.
Now if I go ahead and execute it here you'll be able to see that.
Hey the person the person will drive the petrol car and obviously drive does not return anything over
here.
So that is the reason it is giving you none.
Okay.
But uh, in short, over here you can see that I'm just writing, uh, car one dot drive.
So it is just going to display this.
The person will drive the car.
Okay, perfect.
Uh, if I just remove the print method, then you will be able to see that I'll be able to get only
one message.
Okay?
That is print.
The person will drive the petrol car because this drive method is not returning anything.
But this is fine, right?
Uh, this is very much easy.
And here you can actually see that what?
All things we have basically done.
Right.
We have given a car class, and probably we have defined the init method.
We have defined a method over here.
Right.
And we are able to create the objects.
Right.
This is perfect.
Now let me do one thing.
Now see there are different types of cars.
Right.
Uh, I can just consider this car class is a blueprint of a car object, right?
It is.
This is the blueprint where it has this many number of attributes, this many number of methods.
Now, let me just go ahead and create one class which is called as Tesla.
Okay.
Now this Tesla car that you are probably seeing over here, obviously it needs to inherit from the class
car blueprint itself, right.
Because in this Tesla also I will be having windows, I will be having windows, I will be having doors.
I will be having engine type.
Right.
And there may be some additional feature that may be available in Tesla.
So I don't have to again, go ahead and define this entire thing instead.
What I can do, I can just inherit this car class and I can inherit this properties along with I can
inherit this functions or methods.
Right?
Because I don't have to recreate the entire class completely from scratch if it is inheriting this particular
car class, which is a blueprint, it will be able to inherit all these attributes and methods, right.
So let's go ahead and do this.
So in order to make Tesla inherit car.
So I will just go ahead and write over here as car.
So this is how you basically write.
First of all you really need to write your child class like class Tesla.
And then whatever things you basically write in the bracket, this is basically inheriting that particular
class.
Okay.
Now we will again go ahead with defining our init function because here also we'll be having an init
function right.
So definition underscore underscore init underscore underscore.
So this is my constructor inside the Tesla.
And you know here also we require attributes like windows doors okay.
So we need to pass it during the runtime.
Then you have engine type.
And finally these all attributes are already available over here.
Right.
So they may be additional attributes that may be available in Tesla.
So I will just go ahead and say is self-driving okay.
And this will be a boolean value.
Let's consider this okay.
It's self-driving.
So Tesla you know that it has a self-driving functionalities okay.
Now fine.
Now the main thing is that I need to create the attributes for windows, doors and engine type.
And I know that car already has this kind of attributes.
Right.
So I don't need to go ahead and write self dot write.
What I can do is that I can just use this super keyword, right?
If I use the super keyword, that basically means I'm saying, hey, I'm trying to call the see super
basically means I'm just trying to call the parent class over here.
And from the parent class, I will just go ahead and call my init method.
Okay.
So here what does super underscore init basically mean.
That basically means that we are just calling this init method from the car class.
Right.
Parent class.
Right.
And that is the reason we are just writing super dot underscore underscore init.
Now over here the next step will be that you know what all attributes I'll be getting from the parent
class.
One is windows.
Okay.
So let me just go ahead and write windows doors.
And then you have this engine type right.
Then you have this engine type.
So this three values or attributes I should be getting from this car class.
Right.
So I don't have to again define it.
But what about my cell is self driving.
So I will just go ahead and write self dot is self driving okay.
So let me just go ahead and create this variable.
Self dot is self driving.
Oops.
Self dot is self driving and I will assign it to is dot self driving.
So this is the attribute and this only attribute will try to create inside the Tesla class okay.
And remaining all we are in short inheriting from the parent class right now when we are inheriting
this.
Obviously it is true that we will also be able to inherit this two, right?
So let's see this okay.
Now I will go ahead and probably I've created my init method over here.
And uh now what I will do I will create another function which supports self driving.
Right?
Let's say self driving.
So here I'm just going to write self driving.
And here I'm going to say self okay.
Perfect.
Right.
So this is the method or this is the instance method that we are going to create inside this Tesla cars.
Uh which is the additional thing when compared to the uh, class blueprint.
Car blueprint.
Sorry.
Now, here, I'm just going to, uh, print something and write over here.
Uh, Tesla.
Supports.
Okay.
I can just go ahead and write self dot is.
So here I'm just going to use my f string.
Always focus on using this f string.
Then only you will be able to use this particular uh you'll be able to basically write in this way.
So self dot is driving okay.
So here you have this entire thing okay.
Now Tesla supports self driving.
True or not Tesla supports self driving.
So here I'll say self driving.
So this will basically give me a value like true or false okay.
Now this is my entire Tesla car right?
And this is my child class which is inheriting the car class.
Right.
Now once I execute this now let's go ahead and call this okay.
So first of all I will just go ahead and write Tesla one variant and I will go ahead and initialize
it.
And now what all parameters I need to give windows doors and engine type.
So I'll say hey windows are four.
Doors are five.
Engine type is electric.
Let's say electric okay.
And uh, whether the, uh, whether it supports self-driving or not, I'll just go ahead and write.
True.
Okay.
Now, after giving all these parameters, you will be able to see that if I go ahead and print Tesla
one dot self-driving.
Okay.
And I'll call this function.
Let's see what it says.
So here you will be able to see that it is printing Tesla supports self-driving is equal to true.
And this is what we have given.
So what happens once we call this particular child class with Tesla and all these values.
What it is going to do.
It will just go ahead and call super init and it will pass all these parameters over here.
And this init will make sure that all the attributes, it will go ahead and initialize with those values
like self, dot windows, door doors and engine type.
Now once that is done then we'll go ahead and initialize.
The attribute is underscore self driving which will be assigned to true.
And then I can probably call this particular function not only this.
Now in the parent class I have this drive function right.
So I can also call that see I will go ahead and write test one dot drive function okay.
If I go ahead and call this here, you can see the person will drive the electric car.
Right.
And this is how you can probably see that a single inheritance works like.
Right.
And, uh.
Uh, you understood the main mechanism.
If you really want to inherit any parent class, how you have to probably use this init method, how
you have to call the super init method, how to define the new attributes, how to initialize those
new attributes over here, you know, during the runtime.
And along with that, you can actually go ahead and create any more methods that you want.
Okay.
And this is what is an example with respect to inheritance and this inheritance, I will just say it
as single inheritance because, uh, only one child class is inheriting the parent class.
Okay.
Now let me just, uh, show you an example with respect to multiple inheritance, but I hope you are
able to understand.
Okay, so here will be multiple inheritance okay.
Now multiple inheritance means what?
When a class.
When a class inherits from more than one base class.
Okay.
When a class inherits more than one base class.
Okay.
Now what does this basically mean?
Let's go ahead and create one base class okay.
Let's say I will go ahead and create animal okay.
So animal is one class okay.
And here I'm just going to use my init method.
And here let me just go ahead and write self comma name.
Name is what I'm going to pass as an attribute.
And I will go ahead and create my attribute self dot name is equal to name right.
So this becomes my animal class okay.
And animal class.
Let me just go ahead and create one function which is will be like speak okay.
And here I'm going to use self.
And let me just go ahead and uh, probably, um, uh, you know, print something.
So here I will just go ahead and print.
Okay.
Sub classes must implement this method.
Let's say I'm just going to write this.
Okay.
Now this is done with respect to my class.
Right now, what I will do is that I will go ahead and create another base class.
So this is one of my base class.
So here I'll write base class one okay.
Base class one.
Because see what when a class inheritance inherits from more than one base class.
So this is my base class one.
Now let me just go ahead and create my base class two.
So base class two okay.
And here I'm just going to copy this.
And here you can see I have this pet class.
And I'm creating this init method with self comma owner.
And I'm saying hey self dot owner is equal to owner okay.
Now let's go ahead and create my derived class.
Now derived class will be something like inheriting both these classes.
Now in order to offer a child class to inherit both this class, I will go ahead and create class dog.
Okay.
And this time, in order to inherit both the classes, I will go ahead and write animal, comma, pet.
Okay, so this is how you basically give in comma separated inside this brackets.
Then what I will do from animal I will be taking this name class name attribute.
I will be accessing this peak along with this from pet.
I will be getting owner right.
So here.
If I want to use this owner attribute and name all I have to basically go ahead and right.
I'll go ahead and create my init method okay.
And here I'm going to give self comma name comma owner.
Right.
And here let me just go ahead and write animal dot underscore underscore init okay.
So this is also another way of probably calling it right.
Let's say I want to get the attribute of name from animal.
Right.
So this name is basically present in animal.
So instead of just writing super Dot I can just write the class name dot in it.
And here I can actually give self comma name.
Okay.
So this is one way uh the other way is that again I will go ahead and do it for the other class pet
underscore init.
Underscore underscore.
And here also I'll go ahead and write self comma owner okay.
Self comma owner.
See what is the difference between before the methods that I have discussed here I have used super because
I just had one parent class.
But since here I have two parent class like base class one and base class two, right?
I need to use one attribute from this particular attribute itself from this particular class animal,
and one more from this, another base class.
So what I'm doing I'm just writing animal dot underscore underscore init.
I'm calling the animal init method.
And then I'm passing this particular parameter.
And then I'm calling pet underscore underscore init.
And then I'm passing the parameter over there.
And when it goes to this init method then this attribute gets the value of this right.
This is how the entire flow goes.
Now finally, let me just go ahead and create a speak method.
So I'll just go ahead and write a definition speak.
And here I'll go and say it is a self.
Now let me just go ahead and return.
So return will be an F string.
And let's say here I'm going to basically write self dot name says HUF okay self dot name says HUF okay.
Okay.
And this is what is basically getting printed.
So let me just go ahead and write a return over here.
Okay.
Now it should be working fine.
Now let's see this how this will work.
Now we'll go ahead and create an object.
Okay.
Create an object just to see that whether it is working fine or not.
So here first of all I'll go ahead and call dog is equal to I will initialize from dog.
Here I'm just going to write the name will be buddy.
and the owner belongs to crush.
Okay, let's say then I will go ahead and print dog Dot speak.
And if I go ahead and execute this, you'll be able to see that I'll be getting buddy says woof.
See, there is also a speak method over here, but since we are directly calling it from the dog class,
so this will this speak method will be given more priority when compared to the parent class.
Right.
So that is the reason it is saying but he says woof okay, then I will just go ahead and print my F
owner owner will be nothing but it will be self.
Dot oh sorry dog dot owner right dog oh sorry dog dog dog dog dot owner Okay, so let me just go ahead
and execute it.
So here you'll be able to see that owner is nothing but crush.
And buddy says woof.
So here what we are doing here we are creating two parent class two separate base class okay.
And we are inheriting them from a derived class.
And this is basically called as multiple inheritance.
Okay.
Or we can also say it as multiple inheritance.
Yes.
Now, uh, let's do one thing.
Uh, let's create an amazing use case.
Uh, and, uh, this use case will be about modeling a complete library system, uh, that will discuss
in our next video, uh, where we'll be modeling a complete library system.
Here we there will be using single and multiple inheritance.
So, yes, this was it for my side.
I hope you liked this particular video.
Uh, I will see you all in the next video.
Thank you.
Take care.