Advance Python Series- Multiple Inheritance In Python


Hello all my name is krishnak and welcome to my youtube channel so guys yet another video on the python series and here we are basically going to discuss about the oops concept that is called as multiple inheritance now remember guys we have already seen what is inheritance and all so let's go and check out what does multiple inheritance actually mean now in inheritance suppose if i make a class a suppose i go ahead and make a class a let me just zoom in a little bit and after creating class a what i would like to do is that i would like to provide some information regarding suppose uh let me just remove this parenthesis suppose i'm creating a definition saying as print method one okay inside this this is my method one here probably i can take self and what i'm doing is that i'm just printing method a method is called right a class method is called this is basically my uh function that is present inside this particular class right now what i'm going to do is that after this let me just execute this this has got executed successfully let me also make another class which is like class b and in this we are going to understand what is multiple inheritance suppose this class b is inheriting from class a okay and then what i'm going to do is over here again define the same method probably method one and then probably self right and then what i'll do is that i'll say print b class b class method is called let me just copy the same thing over here and paste it over here okay so this will basically be my b i'll just execute this and similarly what i'll do is that again i'll say class c suppose class c is also inheriting from a okay and probably i'll also define this particular method and after this you can see that i'm just going to write c class method is called now i hope everybody knows about inheritance inheritance basically says that yes if you're inheriting any method from the parent class then you will also be able to access this specific method for going ahead with respect to this particular video this video has been sponsored by an academy so an academy has come up with this unlimited access to all the courses by top educators with an academy plus subscription what is this plus subscription it will give you the access of all the courses by top educators for your specified goal structured courses live regular classes doubt clearing session and regular quizzes will help you track your performance not just this you can also enroll to as many courses as you want under a particular goal don't worry if you miss any live sessions you can see the recorded session of the same class not only this guys unacademy has also come up with this iconic subscription where you'll be getting personal course study planner bile weekly reviews dedicated doubt clearing session personalized test analysis study booster sessions preparatory study materials apart from all these guys unacademy offers a lot of test free test series or special classes one among them is the combat that takes place in every 14 days if you really want to get into this please to use my code that is kn06 to get the 10 percent extra off on every subscription all the remaining information will be given in the description of this particular video okay so suppose class b is inheriting from class a that basically means class b will be able to inherited the properties the methods and different kind of things from class a now here in short what i've done is that i've created class a i have created class b class b is basically inheriting from class a class c is basically inheriting from class a but still we have not come up with multiple inheritance multiple analytics say that suppose if i'm going to make one more class suppose i'm going to make class d and here probably i want to inherited two base class now two base class can be in this particular case class b and class c now see this guys i'm just going to inherit this with b and c because here multiple inheritance is that one class will be inheriting from two different base classes now in this particular case base class is nothing but b and c so again i'm going to define this specific method over here and finally i'm just going to say that class d d class method is basically getting called now this is it ah c is not defined yc not defined let me see uh over here okay sorry i did not execute this i'm extremely sorry i did not execute this now let me just execute this now here you have d class which is inheriting from two base classes and whenever you are inheriting from two or more base classes then it is basically called as multiple inheritance okay now let's call this how do we call it suppose if i go and make an object of d right and then if i write d dot method one okay d dot method one and if i just execute it here you can see d method is basically gotting called right but still in the in the case of multiple inheritance we have to see that okay can we call this methods this method specifically from the c b or a class because see if d is inheriting from b and c which is basically inheriting from two base classes and we know that b is also inheriting a probably we can also actually access this particular methods itself so how do we access it now one thing one very important thing that you need to understand is that with the help of multiple inheritance obviously we will be able to inherit from two different base classes but still the question arises okay right now when i'm calling this particular method one d class method is called but how do i call this method that is c method or how do i call this method one of probably the b class or the a class so in order to do this what i can do is that for this since the method name is almost same if the method name was different probably i would had different methods name like this so let me consider that inside my b i have some other method like this method two right and probably here i'm going to give self and here i'm going to say print b class method 2 is basically getting called okay so method 2 b class method 2 is basically getting called right i'm just going to write this particular message and let me just again execute it or dbc and if i try to make this and if i try to call this this is fine now if i try to call method 2 then i will be able to inherited this right and that is what inheritance basically means now but but still the question is that probably i have all this particular method one method one method one in every classes how do i call a parent method one class so for that what we do is that guys it's very very simple first of all i'm just going to call the class name suppose i want to call this particular method okay method one of class b so what i'm going to do i'm just going to write b dot right b dot method one and here inside this braces i have to basically give my object of this child class when i give this particular object of the child class from the child class i will be able to call this particular method also so what i'm going to write i'm just going to execute this now here you can see that b class method is called similarly over here we know that d is inheriting b and c right these basically inheriting b and c but b and c are in turn inheriting classes so we can also call this particular method so in order to call it what i'll do i'll just write a dot method one and here i'm just going to pass the object that is d which i have actually created so that from the child class i will be able to call that particular method class so here you can see that a class method is basically getting called okay now this is very very much simple right from the d class we are basically going to call uh different different things now what if i really want to as soon as i just call method one from d suppose if i write like this right d dot method one just by calling this i should be able to call all the methods uh from c from b from a that also we can actually do it so let's see how to do it instead of writing that particular thing over here uh you know this all things like b method one of d these basically the object of the child class i can also write something like this so here i'll write suppose i want to call first of all c method so i'll write c dot method one and here instead of providing the object here i'll provide the self keyword because self basically says that yes we are going to refer about that specific object right similarly what i'm going to do i'm just going to call b dot method 1 of self and finally i'll also be able to call a dot method 1 of self so when i write like this then also i'll be able to execute it if you don't trust me just have a look onto this so now i'm just going to call d is equal to d this is basically the object of this particular d class which is a child class and now when i write d dot method one when i write d dot method one you see the output so here first your d method is already called then you go to c method c method dot method one self then you can see c class method is called b class method is called a class method is called now here in short what you are doing is that from the d right even though you are inheriting from two base classes and if this particular base class b and c's are inheriting from some parent class there also you can basically access the method and you can actually call it over here so i hope you like this particular video uh there is also a concept called as multi level inheritance guys in multi-level a is basically getting inherited by b b probably will be getting inherited by c so c can also call the a method it looks something like this if you really want a video for this i can also make a video but just have a look onto this and uh we had actually shown you how you can call the methods with respect to the parent class and all right so i hope you like this particular video please do subscribe the channel if you're not already subscribe i'll see you in the next video have a great day thank you bye.