Advance Python Series- Inheritance In Python
Hello my name is krishnak and welcome to my youtube channel so guys we are going to continue the advanced python series uh and in this particular video we are going to discuss about a very important oops concept that is inheritance now here i'm going to take some examples over here can clearly explain you how the inheritance actually happens how we can write that specific code with respect to python all that thing we'll be discussing in this particular video if you have not subscribed the channel please do subscribe the channel guys press the bell notification icon because i'm going to continue lot of advanced python series lot of topics are there you know after that i've also started now probably in another couple of days i'm also going to start flask framework okay because many people had requested for me to complete flask and django so both are in the pipeline and probably in a couple of days i will be starting flask so let's begin and try to understand how does python oops concept with respect to inheritance how you can implement it with the help of python before going ahead with respect to this particular video this video has been sponsored by an academy so unacademy is back with yet another exciting test gate challenge this challenge consists of 65 questions to be solved in 180 minutes all the questions are designed and curated by best faculties in an academy just not this all india ranking of the test would be provided if you give the test live the gate challenge comprises of all the engineering branches that is mec civil electrical ece csit and chemical so what you're waiting for all the information regarding this is given in the description of this particular video you can also use the invitation code that is kn06 which is again provided in the description of this particular video so please go ahead and enroll so to begin with guys here what i have done is that first of all i have defined a simple car okay so this is basically my car class let's consider that this is my blueprint of the car it will completely make sense because i'm going to make this as a story and you will be able to understand so this is a complete blueprint a car blueprint basic information of the car is basically present over here what all information are there i've just taken as an example over here so i haven't you know included windows doors engine type okay so these are the basic information of the car that we have this definition underscore underscore init underscore underscore this is basically my initialization constructor initially whenever i'm creating the object of the car i need to provide this basic information the next thing is basically a public method this method is nothing but the functionality ins that is present inside the car so here i have just given as an example definition drive drive is a function here i've just given a message the person drives the car now once i execute this and after this i will try to make an object after making an object what i'll do i will provide all these specific parameters that is self windows doors engine type so first of all i'll just provide four then probably doors are five okay and i will say the engine type is diesel of this particular car and i will just execute it right once i execute it this is a car object now if i really want to access any of this so i'll write car dot windows remember this all are public variables i obviously defined about access modifiers in my previous video where i have discussed about public pride private and protected right for protected we basically write it as underscore right for private we basically write it as double underscore and i have also told you when should we use private public and protected right by default if we don't write anything it is treated as public in python it is more like an indication to an other developer whenever we try to define underscore and double underscore right the developers should be able to understand when we can actually access this those particular values when we can modify this particular values okay now over here if i write director of dir of car i will be able to see all my public variables over here easily right now doors drive is basically my function okay the function will also be available over here engine type and windows now the next thing that we want suppose if i write car dot windows i will be able to get it and i'll be getting the value similarly if i write car dot engine type i will be able to get this particular value if i need to call this particular function then i will be using car dot drive i hope this is pretty much simple and this has been covered in my previous videos in my complete machine learning playlist okay i'm getting an error it takes zero positional argument but one was given ah what is the problem over here car.drive okay one one problem that i had made a mistake over here i had to write self okay so here it is now if i write itself then this is basically a method this is a mistake that i have done guys just try to understand if you had actually raised this that is well and good in the description but just try to see why we are writing self because this method is belonging to this particular class so whenever we define an object of that particular class and once we call this by default it is going to treat that this particular object is basically getting called right so car dot drive the person drives the car now this is a blueprint i said that this is a normal blueprint now tomorrow i may come up with another class which is called as an audi car okay so this is my audi car and in this audi car i will definitely be inheriting the car functionalities right so this blueprint functionalities we will be inheriting so once we inherit this blue blueprint from functionalities now the next thing is that we will also create an init for this particular subclass okay and here i will be having all the parameters this is how inheritance actually works guys now this car needs to be inherited by this audi car because all most of the functionalities are almost same right all these functionalities will be reused over there even this particular functionality because obviously a car needs to be driven right and audi can also be driven right so that is the reason what we do is that we try to inherit this car in the audi class so we or is your child class car is your parent class here i am going to write self comma and then we are going to define this three parameters once we define these three parameters after this what is the additional parameter probably the audi car want right so the audi car can have an additional parameter saying that ai ai field i'll say or enable ai okay so suppose this is my additional field probably the audi car once because this enable ai basically says that whether ai functionalities are present in the car or not right so i will write this all things but now you need to note one thing once we are inheriting this with this car and we are taking all these particular parameters we also need to specify that this parameters will be inherited from this particular car object right sorry car class itself so for that what we do is that we if we need to inherited those we need to write super dot underscore underscore init that basically means we are going to call the parent super in its parent init that is the initialization constructor with all these particular parameters with all these particular parameters.
Now i've got all these values from my parent class and then i will say self dot i'll create a new variable which is called as new class property that is enable ai and whatever sorry this should be self self dot enable ai is equal to this particular enable ai because this we will be passing as a parameter right when we are actually initializing this audi car so this is it what we have done guys see understand these three parameters we have accessed from the parent class and the additional parameter that is as enable ai this we are going to create it as a new attribute and this value will be assigned to this that's it and now i can also write new functionalities right ai driven or self self driving suppose i i don't know whether audi has self driving or not but i can write a function and here i'll give a self and here i will give a print statement saying that audi supports audi supports supports self self-driving audi support cell driving right so once i execute it that's it see so easily we have implemented it new functionalities inside rd apart from this audi will be inheriting all these particular properties apart from this audio will also inherit this particular functionality that is your drive so now it is all together having its added new features also one is enable ai and one is self driving now let's initialize audi so for the initializing audi i will say that audi 1 okay i'll i'll just give you a model of audi audi q7 is equal to audi and here i will say that okay it has probably five windows five doors and the engine type is basically dizzle and enable ai is basically true okay i'm just going to initialize in this particular way and now if i go and see my direct of audi audi q7 here you will be able to see all the parameters doors drives enable ai engine type self driving windows all the functionalities of the parent class is clearly given over you guys so you can access all these particular parameters and see to it so let's see so if i write audi q or dq7 dot windows here you will be getting five if you write audi q7 dot uh doors here also you will be getting five if you write enable ai what is it true if you want to see the type of it it is basically boolean right boolean simple you have got all this particular information is an amazing way if you really want to utilize this functionality also now see this drive is coming from its parent class so if i write drive then here you can see that the person drives the car and then you can also use self driving and basically you can also call this particular functionality like this you can have any number of multi-level class also guys i'll also be showing you about multiple inheritance in my upcoming video but i hope you have got an idea about inheritance a very important concept guys it will definitely help you to reusability of the code simplicity of the code and do a whole lot of thing you know so this is the most important thing that is inheritance and still there are a lot of functionalities with respect to object oriented programming like data encapsulation abstraction and all so we'll try to cover that in the upcoming videos and apart from that i will talk more about inheritance as we go ahead but i hope you like this particular video please do subscribe the channel if you're not already subscribe i'll see y'all in the next video have a great day ahead thank you andrea bye bye.
Now i've got all these values from my parent class and then i will say self dot i'll create a new variable which is called as new class property that is enable ai and whatever sorry this should be self self dot enable ai is equal to this particular enable ai because this we will be passing as a parameter right when we are actually initializing this audi car so this is it what we have done guys see understand these three parameters we have accessed from the parent class and the additional parameter that is as enable ai this we are going to create it as a new attribute and this value will be assigned to this that's it and now i can also write new functionalities right ai driven or self self driving suppose i i don't know whether audi has self driving or not but i can write a function and here i'll give a self and here i will give a print statement saying that audi supports audi supports supports self self-driving audi support cell driving right so once i execute it that's it see so easily we have implemented it new functionalities inside rd apart from this audi will be inheriting all these particular properties apart from this audio will also inherit this particular functionality that is your drive so now it is all together having its added new features also one is enable ai and one is self driving now let's initialize audi so for the initializing audi i will say that audi 1 okay i'll i'll just give you a model of audi audi q7 is equal to audi and here i will say that okay it has probably five windows five doors and the engine type is basically dizzle and enable ai is basically true okay i'm just going to initialize in this particular way and now if i go and see my direct of audi audi q7 here you will be able to see all the parameters doors drives enable ai engine type self driving windows all the functionalities of the parent class is clearly given over you guys so you can access all these particular parameters and see to it so let's see so if i write audi q or dq7 dot windows here you will be getting five if you write audi q7 dot uh doors here also you will be getting five if you write enable ai what is it true if you want to see the type of it it is basically boolean right boolean simple you have got all this particular information is an amazing way if you really want to utilize this functionality also now see this drive is coming from its parent class so if i write drive then here you can see that the person drives the car and then you can also use self driving and basically you can also call this particular functionality like this you can have any number of multi-level class also guys i'll also be showing you about multiple inheritance in my upcoming video but i hope you have got an idea about inheritance a very important concept guys it will definitely help you to reusability of the code simplicity of the code and do a whole lot of thing you know so this is the most important thing that is inheritance and still there are a lot of functionalities with respect to object oriented programming like data encapsulation abstraction and all so we'll try to cover that in the upcoming videos and apart from that i will talk more about inheritance as we go ahead but i hope you like this particular video please do subscribe the channel if you're not already subscribe i'll see y'all in the next video have a great day ahead thank you andrea bye bye.