Transcript for:
Introduction to Object-Oriented Programming in Python

[Music] but at this point we have seen the language fundamentals of Python right we have seen variables how to pass a variable to a function we have seen function right how to define function how to call a function and then we moved towards module and now we know how to get different files now once we have talked about the language fundamentals let's move towards the concepts now one of the concept it is very important is oops which is object-oriented programming now one of the unique selling point of Python you know why pythons are famous because of this thing Python supports all different programming paradigm you know it supports functional programming it supports object-oriented programming it also works with procedure oriented programming now we have talked about procedural right what is position now is whenever you work with Python we defined functions why these polar function from a function so if you want to create a software you will break down your project into small small part and those modules small modules will be functions now Python is also functional programming so functional programming basically means if you can achieve a certain task implementing functions as our mathematical functions where you will not manipulate a DITA which is instead of changing data you will still accomplish a task one of the way is you can pass a function to a function you know we have seen this are going to be have a very deep concept of functional programming we'll talk about that later but time being procedures and functions which is something we have already done for example lambdas now we are going for a concept of ops now which is very famous in the industry you know so if you are walking on big software's enterprise projects you need to think everything in a format of objects now you are thinking why objects we would happy with functions right now see even from start itself out I'm talking about objects right maybe in some programming I said okay this is the object integers an object but then we were skipping that part what is object and why it's important see mentally if we talk about programming world what we do is we try to solve a real-world issue with the help of virtual world solution right so example programming software's everything is virtual world now in real world we all use objects for example if I want to record this video I need a camera right if I want to call someone I the food if you want to type a code you need a laptop right so for everything you need object so in real world everything is object in fact person's right example if I have a company if I want some work to be done of course I need employees right and for me every employee is an object right so an entire object is using a laptop object so some employer object needs a AC object right that's how things works everything is an object even human we treat them as object now what object will have object will have two things every object will have certain attributes and every object will have certain behavior now when I say attribute you can imagine them as data or you can say properties example my height is one attribute my age is one attribute my name is when I want attribute the company I work for this one attribute right everything is those those things are attribute and then it comes my behavior and talking and walking and dancing all this our behavior right so our actions defines our behavior and then we have attributes which we know in other terms you can say as an object I know something as object I do something right so I do something based on something which I know right so this is object object will have something that you can store either an object will have some behavior now this data which we normally use those are variables right we have same variables so if you want to store something an object we need to define variables if you want to define the behavior we need to use methods that's something new now what is method we have done with functions right so functions in object-oriented programming they are called as methods rights are so simple we have done all those stuff the only change we have now is we to think in an object term see object-oriented programming is not just about different syntax it's also about the way you think so now if you want to achieve anything in programming you need to think about objects so the moment you say you have a complex problem think about the object think about my simple objects and how can how can you connect them right because in Oaks we have so many concepts one of the concept is object which we are going to see now we have a concept of classes we have a concept of encapsulation abstraction polymorphism I know weird words right but don't worry we will be covering everything in detail in the subsequent tutorials so there's one thing which is important discuss here about pluses because we have an object and we have a concept of class it doesn't matter which language you work on maybe Java C++ or C sharp whichever whatever language which uses oops concept you know we always use this thing which is object and class together but why why they are so important of course objects are important because whatever you want to do can be done with the help of object but why Trust is important now think about this whenever it whenever you see an object in real one example this camera of fans or the clock right all these things are manufactured right so in some factory they have been manufactured or maybe you can talk about a for now look at this one this is Motorola g3 okay now it's not the only phone available he writes a lot of people they have this phone so that means we don't have one object of moto g3 we have thousands or millions of object I'm using any number here so imagine if we have five million of this type of phones which is Moto G 3 now of course someone must have designed it right and all these phones have been manufactured somewhere maybe maybe you can say all these phones are made in China ok so we have a factory in China who is producing all this phone but then what Rama says is there phone right of course we are not we don't say this is a China phone we say this is a Motorola phone or we don't say let's say the manufacturing is an in India which so that we don't set your Indian phone we say it's a Motorola phone why because manufacturing is not important you can manufacture stuff anywhere what is important is where it has been designed ok so this is designed by Motorola but even if you talk about this phone this has been designed once and then they have manufactured multiple times and that design in programming all in oops we say plus so in our class you will write a design of the object imagine there's some factory in your machine who will create these objects ok so you have to provide a design so that you will get the object so that means class is a design and objects there are real stuff right there as well entities you can say or you can use another term Palace instance so you can imagine this is an instance of a class that's awesome like so pointy remember class is a design or you can call them as blueprint so if you have a class you can manufacture thousands of billions of these devices so that's about as an object in the last video we have talked about this theory of objects and a class right so of course before creating an object you need to create a class right because plus is a design for the object and if you don't have a design how can you build something in fact even before a building a tower you need to create a blueprint of it right and that's the same case here so if you want an object you need to create a class so what I'm talking about is you know let's say I want to work with computers so I'm a person I want to work with a computer so of course I want an object of a computer right so let's say I'm a programmer and of course I want to work with a computer so I will say comp one so this is my computer but see the thing is till this point we have work with integers we have worked with float string now all those are inbuilt types right we don't have any type as computer so of course we need to create our own type so we have to be our own class for it so if you want to use a computer you need to define a class here so how do we define a class the way we define a function is by saying def and function name the way we define a class is using a class keyword so we have to say class and this class will of course have a name in this case I will use a name as computer ok now this is how you define a class you have to specify a Poulin now after polar this is your suite right so this is where you will mention your stuff now what stuff we can put in a class two things the first thing is the attributes the second one is the behavior Oh now when you say attribute it is your variables okay the way you create variables and the next thing we'll be having here is behavior which is methods of what is methods functions are not the functions which we are using from a long time but here we call them as methods so next time when you are calling a function but that functions are part of a class instead of calling them as functions you have to say methods right ok now let's define some method see now what I'm talking about if I say let me get a method here it's a death I would say convict because every machine has a conjugation right so I will say convict we got that self keyword that that's we're gonna sell because yeah I'm not typing it okay so I mean the moment I say bracket it says self we'll talk about that cells later but time being let's keep it there you can imagine we don't have any selten okay but time being imagine we don't have anything like itself okay and here we'll say we gotta come to a place and then we got a Quantic method or you can say function but try to move from that that function term to method tone right so we got a method here which is convict and here I'm printing the config of this machine and I want to say hey this is I 5 machine with 16 GB RAM and it has one data byte header now this is very common config not which we find nowadays right and that's what I'm doing here so we got a class computer the counter method in that example here I'm saying comp 1 is the object of computer but we do not simply do that right we cannot simply say comp 1 because no one will understand what this comp 1 is example if I say this available a what is a type of this variable right now since Ben Python we don't have to statically mention the type of it so what we can do is when you say a equal to 5 now the type of a becomes integer right if I say is 5.5 the type of a becomes float if I assign a value of string type if I say 8 even if that 8 is number we are putting that in single quote right that becomes a string what about comp one called one here don't have a type we have to mention hey this comp 1 is an object of computer in that case it will say equal to computer and you have to give this round brackets now if you are coming from another you know from different programming pattern like Java C sharp we have a concept of punch drop dead right so this is a constructor there so this will give you the object of computer now how do I know that if you remember in one of the video we have talked about a type function right so you can use type function here so I will say print I will define type print the type of porn one let's see what happens what it gives you so if another score it says hey this is a class okay down we have different keyword class and this belongs to a computer that's important ok so it gives you the module the model name is main because we arranged this quote from here and then we have a class for this computer remember we when we worked with string as well string gives the same thing so if I print the type of a it also say the same thing can you see that it says class STR right so we are using this concept from a long time but this is where we are defining it now so string STR is also class the same way computer class the difference is HDL is an inbuild class computer is our class okay that's how you define it so you say class computer and then we have def conflict in fact there is one more things remember when we worked with integers we have also said that integers are also object that's right here I will say X equal to 9 and let me print the type of X can see that when I print the type of X it says plus 8 so even this integer this X is an object of integer now right so other thing is object in Python right so there are some inbuilt objects and there are some objects which we create and the way you create your own class is by saying class computer whatever class name make sure that you don't use any bigger class name because it will give you error all right or maybe you might face some bugs so we have so in a class we are defining a function config and then oh not function method font config and then we are printing just a statement now question arise how will you call it so let me remove this all this extra stuff which we are doing so we got a class because the object I want to call that config now what you think how will you do it so normally when you have function you you mention the function name right here in this case it is perfect but you are getting an error why error so if I go back here it says unresolved reference or that sphere what is giving you unresolved because of course normally till this point we were mentioning the function as it is wide open but now we have written that function or a method inside a class so in order to access that we have to mention the class name and the class name here is computer so we have to say computer dot conflict important so steps remember the steps you have to mention the computer class which is class name and then you have to say conflict so let me repeat if you want to use a method we have to mention a class name first which is I mean way it belongs to computer and then you have to mention config and if I run this code oh we got and but why to think about this one class can have multiple objects right one object to object five objects maybe billions of objects right in this case we are using only one object which is comp 1 the thing is this config function order method will change its behavior based on the object right because different objects have a different behavior because depend upon what they know they have a different behavior see in this case I'm not depending on any data right but maybe there's a chance that this conflict will depend upon some data and every object will have its own data right ok we'll talk about that later but every one which is different right if I'm causing conflict for which object I am doing that so if I say hey walk but who mama I'm supposed to say walk serve to mention here we walk hey Mukesh Blanc hey Kiran walks up I have to mention the names as well right in the same way when I say computer con forget to also mentioned for which object you are talking about and I am talking about comp 1 so I'm saying hey I want the configure forum 1 you know luckily we have only one object here so there is no confusion but let's say if you have thousands of objects you have to mention the object name as well so we are doing comp 1 here so if I love this code now or it works you can see that we got the output so we are calling conflict and we are passing comp 1 and this fourth one is a parameter right is the argument so when you call config you're passing this comp 1 as the argument and it is going in self so what is this self self is the object which you are passing again this thing will get more clear once you see for the videos right so this is a complex concept if you are learning oops for the first time but step by step everything will go inside your brain ok so this is the first player again in the next video we'll talk about some more layers to it we'll add more layers and then I will make sure that you get everything about it ok let's create some more objects ok so I would say comm 2 is poised to computer right so we got count 1 we got comm - so in total we have two object which is comp 1 and pong - but we have only one class which is computer if I want to call config for pong - what you will do you will say computer dot config and this time you are saying hey I want to call convict for pong - so in the first rinse I'm saying Mukesh walk right or let me say hey human walk in bracket and passing Mukesh I would say hey human walk in back and I'm passing Davi in the same way here I'm saying hey computer show me a conjugation and I'm talking about com1 hey computer I want your quantification for pong - so from this quote you can see we already same data because at this point we are not changing data for different objects we can do that data so this is one way right if you want to call conflict we have another way if you think about this this looks good right this looks clean because we are saying hey config method belongs to computer and then we have to pass the object images from one but there's one more way what you can do is you can mention call 1 dot conflict now in this case you are using the object itself to call the function so you're saying comp 1 dot config but question is we are not passing it right example let's do it for com2 as well I will say con 2 dot conflict and you can see this time I'm not passing anything inside conflict and still 12 so you can see the output it works record 4 times the same data because the first two is because of this lines and the second next two is because of this line right but why it is working it's because this is another behavior of it so when you say comp 1 dot conflict you are calling conflict see we are specifying of which class it is rights of bound one Valastro computer so now we know config belongs to computer the moment you say comp 1 dot conflict when you are calling it behind the scene this conflict will take pump 1 is a parameter so behind the scene conflict will take that cop 1 as an argument and it will pass that in self ok so in most of the codes you will see this type of syntax not this one but I have done this just to explain you how this syntax linked like ok so how this exactly was but normally we use this syntax the second one and that's why whatever we have done till now example if I work with a variable of in size if I say equal to 5 and if I say a dot so whatever function we use your example I want to know the bit length now in this thing you can see if I say control space you know you can actually should see the documentation by clicking on this control and click on the function name so press control + click on the function name you can see takes to the function it says bit length itself so as a definition it is accepting self but when you are passing it when you're calling bit landed we are not passing it so this is an object but it goes as a parameter so self is the object which your passing will discuss more about that once we start with variables and then attributes all the stuff but time being this is how you create a class and that's a trait an object now in the last video we have talked about oops concept right we have seen what is object and a class let's explore it more like remember when we talked about objects object will be having two stuff right one is it will be having some attributes and it will also have a behavior now when you say attributes those are variables right we can create variables and the behavior is your methods and then methods are almost same as functions but in oops we call the mass methods right now here we have to work with methods right we are not worked with variables yet so what I will do is just demonstrate that let me just remove this thing here because if we know that we can Paul config using the object itself or we can call it with the above class by passing the object now syntactically we normally go for the second one which is calling the method as from the object it makes much more sense right so we remove that part and so once we call the object let's move this a as well just to clean it up top here now what I will do is I want to have something variables now when I say variables what I want to do is I want to have two variables one which defines the type of CP you are working with and the amount of RAM I have example if we talk about any computer this two are very important why the CPU which you are using and RAM may be 8gb RAM or 16gb ram and in terms of CPU we have let's say I three i5 i7 or maybe some AMD cities ok we have different parameters as well but just to keep it simple let's only focus on two things CPU and grant capacity now when you say we have two variables ready to define that so we can use a special method which is available in a class now when I say special you know right so the moment you say special we have talked about special variable which is understand this code name in the same way we have special method which is in it and as I especially it will be having underscores as well so let's define that so we'll say def underscore underscore in it you can see that we are getting suggestions as well and the moment I set enter you can see we have the function name or the method name as in it and we are passing self now see itself is not something which I'm typing that is coming automatically so it's compensate to have those argument okay with itself I mean in here we can do something normally we use you need to initialize the variables okay and that's what the name itself in edgewise right now if you are coming from C C++ or Java background we use constructor right so you can imagine this is our constructor but then it is init method the advantage here is normally see if you want to work with conflict you are supposed to call country okay so it's composite' for you to call conflict otherwise it will not get executed the idea behind in it is it will be getting called automatically example if I go back here and let's say I want to bring something and I will try it in in it so you can see I have defined a function for a spin it and not calling it okay so anyway the code I'm not calling in it I'm calling country because I want to execute it the moment I run this code you can see it prints in in it and that do it prints two times now why two times because for every object it will get called once so you can see in total I'm reading two objects one is com1 one second is com2 so we've got two objects and it will call any two times so this is the object creation so when you say computer bracket it will instantiate we to Paul in it for you now I want to pass some arguments okay I want to pass CPU and RAM so in the constructor itself you can pass that so in the computer bracket itself you can pass it so I can say I have a CPU here which is let say I five and the amount of RAM we have is 16gb the second computer we have let's say reason three and the amount of land we have let's say 8gb so we have two computers with two different CPUs right and now how to accept this values so of course you have to pass this value see all right so have to say self comma you have to pass two values you have to accept these two values so you have to say CPU comma Ram okay that's how you accept it now I know your question your question is we are passing two parameters right now we are passing two arguments one is CPU and second is RAM and we are accepting the arguments here so the thing is you are actually passing three arguments here remember we are also passing comp one the moment you say computer in the packet you are passing that comp 1 by default the same thing happens with contig right when we call conflict we are not passing or the object itself but it gets passed automatically the same way I mean you say computer bypassing two parameters you actually passing three the first one is the object itself the second one the value the first value here and then Ram okay that makes sense but will it work so the thing is every object need to have a value because this is just an argument now so CPU and RAM is just an argument if you want it to be a part of your object you just say because say we have to assign this value to an object right and the object here is self right so I'm going to say self dot that's why it right so this pump one this is our object right so we have to say self dot CPU is equal to CP u again there is no compulsion that you should be having same name you can have a different name that's fine so you say self dot Ram is equal to Ram so whatever value are passing here which is I five goes to the CPU as an argument it will be assigned to the object with itself so this self we can imagine now so in the Python you can see we had two objects here so in total we got two objects each object will have its own variables okay it will have its own CPU it will have its own ramp and then whatever either you pass in the arguments it will be assigned to those objects okay so we have two objects two different values and now in the conflict instead of printing this stuff I will say CPU comma ran that's what we do oh we caught an L what's wrong here so thing is this CPU is not a local variable by the CPU belongs to an object and how do we refer to object it's very simple you have to use self right that's why you are passing it so I'll just say self dot CPU and here as well you have to say self dot Ram and that's the idea behind passing this cell right there passing self so that you can use it to fetch the values so you can imagine your methods and your data works together and we have a name for this concept again we'll talk about that name later surprised but we are binding our data with every method so one object will have its own methods and its own variables right so they are working together and now let's run this code and let's see what happens so the moment you learn this code you can see it says config is I 5 16 and config is raisin 3 8 so that's the idea that's how you create a variable so every object will have its own values amazing in the last video we have talked about OUP's concept right we have talked about objects we have talked about classes and then some way we have talked about in it right in this video we'll focus on two concepts one is constructor and itself but hold on we have seen self before right well let's try to expand more of it okay because self is something which is very easy to understand at the same time it is bit confusing okay so we'll try to get that and then we also talked about constructor now in the last video we have talked about in it right now it is actually a constructor but why this first constructor will we see that here and why it is so important so what we'll do is let's remove this thing and let's see a computer class again let's go back to come to class so this is a plus right and of course every class will have an object right as we mentioned we have a concept of a design right so this computer class is a design and then we need to create an object of it so what I will do is I will say C 1 is equal to computer now this computer here is an object white I mean this C 1 is an object all you can say C 1 is referring to the object so what happens you know in your system we have a special memory called as heat memory now inside this heap memory you will get all the objects right example the moment you say you want an object now if you remember we have talked about integer string everything is object in Python right now in this scenario here this object will take some space in your heap memory now every space will have some address right we have talked about ID function remember so we can use an ID function here and we can print the value of C 1 so we can print the address so of course this object is created somewhere and we to print the address as well and the way you can print the address is by saying print I want to print the address of C 1 let's do that oh we are getting an error knife remember we have talked about this stuff so I'm keeping this class empty right we cannot do that so let's write pass because I'm not mentioned anything in the class here so let's write pass let's run this code now and you can see we got the output as 1 5 5 with the number right now this is address okay so this is the address of that memory now what happens if I create another object let's say if I say C 2 equal to computer now we are cutting another object right so in total we call two objects so will it take different space or will it be accommodated in the same place of course we have talked about this before when we talked about integers and location if I love this code you can see we called different addresses and again every time you on this code you will get different address right because every time you run this code it will create a new object for you so this time we got two new objects now if I learn this again we will get two different object not the same object again right because this new execution so point remember is every time you create the object it will take two different spaces now question is how much space it will take maybe one and B maybe 1 KB maybe 5 KB so who will decide and who will allocate the memory now first of all the size of the object is depend upon number of variables we have or the attributes remember if this class has five variables of different type it depend upon how much data you pass it will change the size of it ok so the size of the objects depend upon this size of the variables and number of variables but who is responsible to assign that memory or who is responsible to calculate the memory and that's your constructor now this thing here which is computer blankets this is your instructor right so whenever you write a constructor it will call the init method for you of course you don't have to politics it will be called internally let me define some variables here how do we create variables right so how do we define a variable for the object now in this case if you want to find a variable we have one choice which you can define a function which is in it again we have seen that so this is the init function right now it is a self but as of now we will not talk about self which will continue here and I will use two variables again we haven't talked about this but let me just do it once again I was a self-taught age I want to have two attributes one is name I was a name is Naveen and age soon say it is equal to 28 so we got two things we got name and we got age now both the object will have the same values right so you can imagine we have two objects and both the object have the same value if I want to change it if I want to change the value of one object the way you can do that is in fact of printing IDs I want to change it now because of course why this will print different now we know that so we say c1 dot I want to print name of c1 and I want to print the name of c2 as well so what will happen will it bring the same data or different ATAR that's a question so let's run this code and you can see they are printing Naveen of course right because the moment you create the object both will have the same value because that is what by default are getting if you want to assign your own values you have to choice here what we can do is before printing it we can change the value for C 1 because the C 1 dot name is equal to Rashi so we are changing the name right and now if I than this code you can see we got Rashi and we got Naveen so of course C 1 is different objects it is different object and we got different names so that perfectly makes sense we can change the age of C 1 we can say C 1.8 is less a 12 record washi with H 12 and of course we are not printing H here but we can't do that so we can change the value of one object from different objects I'll of course that to different entities so remember this we got two different objects and both the objects will have different variables okay you can assign the values that you can change it your wish now question arise why do we need this self so what I will do is just to explain that let me take one more better here we'll say def update okay so I'm changing the value of something and the moment I say update I want to change H I will assign a new age and we say age is the setting so we are changing the age now right so we've got a method which will change the age totally now I want to call update now how do you call update with your class name or with the help of object name let's try with objects i will say c 1 dot update now see you have two different objects right so but these program execution right so exhibtion we start from here you got the object you got the object you are changing the names and everything is fine the moment you say see when dot update your pointer which is your focus will move towards this update function of update method so it is executing it right update will say okay I'm being called I belong to a class computer my job is to do something my job should change the value of age but hold on we got two objects which one you want to change right because we are calling update and we are not mentioning which object and talking about because when you this update we have not mentioned is it c1 or c2 we have mentioned that by calling it so we are saying save on dot update but we are not passing anything in the banking so when you are calling it when you're calling update how your pointer will know which object I'm talking about is it the c1 age or is it the c2 H and that's where you need to use itself so this self is a pointer or against itself is directing to c1 or c2 so we have two objects this self will tell it either won't see when or c2 based on what you are putting so if you're saying c1 that update then in the bracket it is passing c1 so self will be assigned to c1 okay so that's the importance of that self it is very important because it is referring to the object if you have ten objects and if you want to refer to one object you can use self so it is the current instance you can say even if you are ten objects at one point you are working with on one object that itself I hope that makes sense so that is self right so we have talked about constructor and self here let me show you one more thing what if I want to compare two objects here okay so I want to compare let's say we have c1 c2 and both have same names right so they have same name and they have same age but let's say I want to compare the age so I will say if c1 is equal to equal to c2 so I want to compare two people based on their age if their age is same I want to print their same so I would say c1 is equal to c2 print they are the same so let's do that so I want to check if both the objects are same then I will print they are same but then I don't want to compare the object address right I want to compare their values I want to compare the age name doesn't matter if name is different what is what should be same is age now in this case if you cannot simply say C 1 equal to C 2 because we don't know your Python don't know how to compare so in this case what you will do is you will use a separate function to do that I will say hey I want to compare C 1 with C 2 so I will say C 1 compare and you can pass C 2 because we are comparing it well again this compare is not a inbuilt function so if I click on this you can see it says cannot find declaration to go to something the thing is compared is not in build method we have so in order to work with compare you have to define your function you have to say they're compared and in this combative course you have to pass itself and then you have to also pass one more object which is of c2 right so we are comparing two objects right so we have to pass self so what is self here so this c1 becomes self and c2 become C two again you can change in the name it's your choice right we can say other right so you're comparing self with other so c1 becomes self and C 2 becomes other now I see one becomes self is because c1 is calling it so c1 is comparing itself in c2 you can reverse it okay you can say C 2 dot compare C 1 in that case C 2 becomes self and C one becomes other right and now once you have this self another you can compare if self dot H is equal to equal to other dot H you will return true else return false you could have done that in one line but just to make it look good I'm doing this and now you can compile it right so they don't have same age because we are changing it of table T let's remove the update part here let's say they have the same age right let's run this demo and you can see oh we got an error we forgot to put a colon there my bad let's run this code right so you can see it is printing they are same so that same right now what is they have different age so what I will do is once once once I quite see when I will change this age of c1 I will say a H of C 1 is 30 anyway we are not using update here and we can say run demo and you can say this not printing they are same we should 20 as well as well so that will see is it different let's try it they are different and let's run this code can see that we got they are different now since that age is different we are getting that different C names are same what is different is age so we can compare two objects by defining our own function which is compact in this case or method in this case but you know this thing compact takes two parameters the first one who is calling it and second one whom to compare right so here in this case we are saying C 1 dot compared with C 2 so C 1 becomes L 0 becomes other this is very important I know it is bit confusing at the start but the moment you start working on it it becomes very easy now we know how to work with glass and object widened if you have also seen how can you create a variable inside an object of course right every object should know something but when it comes to medieval in ops we have two different types of variables the first one is instance variable and the second one is a class variable all you can call them as static variables what is the difference between them now think about this let me just remove this coil okay whatever we have done till now so let me just clean this up okay so it will start from a plain slate so here let's go with another made example let's take a glass Palace car okay if we all love cars right so let's say we have a plus card and of course every car will have different variables right of course you can mention the company name you can mention the type of engineer working with and the mileage what it gives you so we have different variables okay so what I will do is if I want to use those variables here we need to use a function s in it so you will say init function and in this you can define those variables right I can say self dot mileage and I will say mileage is let's say 10 and then I will say the company so company would be let's say B and W any line um company term doesn't matter so we have this two values right now these two variables are called as instance variable so we talk about mileage and Comm these are instance now why is our instances because as your path changes as the object changes this value also change by default the value is 10 and Beyond W but you can change it right example we have done that before just to give you a glimpse I will say the first one is c1 and this is car that's the first object I got c2 which is again a car that is we got c1 c2 now we got two different objects and both the object will have different variables by different area for the variables and now if I print the value so I will say see.com and i also want to print C 1 dot mileage so I want to print both for the both the variables so you can see because even can't see when mileage C to come see - mileage that's perfect and I found this quote of course it will print same values because they are same but can you change it yes we can so I will say if I change for C 1 so let's say C 1 dot mileage so let's say C 1 - changes from 10 to let's say a this is what happens you know with some cars and it's not this car you can see the values now so of course books are different and they will have different values but what if I want to create a variable which is common for all the objects of course this variables which are instance variable they are different for different objects while if you change one object it will not affect other objects what if you want to have a variable which will change it will affect all other objects example the number of wheels in a car is full right by default is 4 right but what if in future we got a new concept and they're saying hey now this time we'll have 5 tires or five wheels I don't know why we have five use but let's imagine in this case what you will do is you will define a variable outside in it okay because if you create all if we define the variable inside in it it becomes an instance variable if you define a variable outside in it and inside a class of course it becomes a class variable let's say I say bills so let's say these are by default food of course will be four right and if I print the wheels as well now how do you print wheels the way you can print is very simple you can say C 1 dot wheels and here as well as says C 2 dot wheels and let's run this code now and you can see we got the output we got four and four so if you want to access wheels you can you say C one touch wheels and C two got wheels C's off using the object name we can also use classname because see if you talk about mill and Comm it is specific to the object right but that's not the case with wheels wheels is common to all the objects so every object can share the same value of it so we can use object name or we can use classname both works so you are getting value for right what if you want to change the value of it if you want to change the value you can come here and you can change the value see what happens you know in your memory you have different namespace now it is namespace the place where you create an object or the variables at that was namespace so we have to double namespace one is class name space where you will store all the class variables and then we have a instance name space where you will create all the instance variable so this variables which is smell and calm they are instance light so they will belong to instance name space this wheel belongs to class namespace right so if you want to work with wheels if you want to modify it you have to use a class thing you have to say card dot wheels and you can assign the value to five now the moment you change the value of these it will affect all the objects right because they are share so this wheel is shared between all the objects and now for this code you can see the output is five and five so that's your plus variable so the point remember is we have put our variables instance variables and class variables or in fact class variables also called as steady variables now in this point we have talked about different types of variables right we have seen instance variable and at last variable we can call the math static variables right now in this video we'll talk about methods and different types of methods now we have seen that right when I talk about objects object will have two things right the first why it will have is variables and second one is methods now variable to store data and methods for the behavior right so if you want to perform some operation that's where methods comes into picture as human eyes well we know something and we do something right so those are your variables and methods right now here we talk about methods we have different types of methods now basically we have three types of methods the first one is instance method the second one we have is class method and then we have static method or hold on when you talk about variables we only have two light because plus and static behavior same way in variables but hold on in variables you get the same thing right in variables we have class variables and static variables which are saying that's not the case with methods okay so class methods and static methods are different in methods okay so let's get started so understand this concept we'll take an example so let's take a class here and within this class as student so in this student class we'll be having multiple variables right now of course we talk about student we have names we have roll number and then we have marks at this point let me only focus on moms okay so what I'm going to do is let me create a function which is your init function because I want to declare some variables so it says self dot now I want to go for three variables here one is marks one and marks on I want to pass this value from the user okay of course you can define your own values here but I want to pass the value when I create the object and the way you do that is by saying self comma you will pass m1 m2 m3 I want to pass three variables and one value will be assigned to the object by saying self dot M 1 equal to M 1 I will go to same thing for em - I will say M 2 and the same thing for M 3 right so we got M 1 n 2 and M 3 now we got three variables I want to create certain objects here ok let's create objects for this I will go back here so let's create the object here I will say s 1 is equal to and as usual you can create multiple objects but time will go for s 1 equal to I will say student that's the first object and if you get when the object will say s 2 equal to student so basically we got two objects here one is s 1 and s 2 I also want to pass a value to it right because we are not passing it so let me pass any value I learned the value are not actually saying what I'm typing here but then we got three values there and here as well I will go for any random value that doesn't make any sense to see that so we got this one is white so first one caught 3467 and 32 or second strand got it is 732 and 12 now what I do is of course this variable com1 n2 and n3 those are instance variables right we have talked about that we at this point we have not created any methods let me just do that it is like before going for methods let me also create a variable I study very well this time and the way you create a static or a class variable just by defining or declaring the variable outside the constructor right so here I will say I have a variable and will in this variable as school because of course right a student belongs to a school right on a college at this point all these students belongs to the same school which is an ESCO imagine if you have that is a university will be having a school so we got to school them as well right so you can print those values by simply saying in fact you can print those values here as well you can say X 1 dot m 1 and X 2 that and what we can do that what I am concerned about here is I want to know the average of marks okay so I've got this 3 variables right I want to perform operation how do you find the average Y so you can the logic of average you can define by yourself you know everyone have their own way of doing it I will be doing a very simple calculation so if we create your method called as def method called average so when I say object dot average it will give me average marks okay so I want to return the average marks and the way you do that by saying if then you want to it in the value and here you will simply say m1 or not just m1 you have to say self dot M 1 plus self dot M 3 so we have to divide this number by 3 that's how you find the average right and we know the stuff right that's how you find it I've got multiple numbers so we got the average and the value okay so now this if you focus on this average this is an instance method how do you know that sign is just method because we are passing self now when you say self it means it belongs to a particular object right of course we are defining that in a class but it works with the object and that's why you're passing self so if you want to call average you have to say s1 dot average all you have to say X 2 dot average because we are passing self we cannot simply say student dot average because we're not using object there but in this case we have to say s1 dot AVG that's how you do it so I have to say s1 dot ABG it won't give you the values I will print the value here I would say plain the average here so let's on this Kwan you can see it works we got the average of four students you can do the same thing for suggestions you can you stop saying s1 dot average you can say s 2 dot average and if you want this code or you can see the code Davis for second student as well and they all say Milo I okay so after changing value because different average you know so this was just a coincidence you know I've given some values and then they were giving you see map this right so yeah so you got a different average for this one and different average for extra dots that works now what we do is so this method here this average is an instance method because it works with the object now in instance itself we have two different types okay the first type of instance which we use is called as accesses and the second type we use is mutated so we have accessors and mutators now what do you exercise so if you are only affecting the value C I'll geometrically talk about this method there is only responsible to work with variables right which is your instance variables so if you want to just fetch the value of the instance variable we will be using accesses if you want to modify the value you will use mutators so the way you do that example if you want to fetch the value of M 1 so if you you want to know the value for M 1 you can fetch directly you can say print s 1 dot and 1 you will get the value like this or instead of using the variables we should actually use methods so what we'll do here is we'll say def and we'll define the function as get m 1 so this is a get m 1 and which and when you want so you will say return I want to return the value for self dot M 1 this is how you do it so if you want to value for every one you will say get M 1 and you got the value normally it's not compulsion to have that get keyword that you can also say m 1 that's fine but when you like a col conventionally you simply specify I get there so anyway you say get methods or in fact to set the value we use example if you want to set the value for m1 you will say set and one of course we are passing that normal constructor so we can say said and well you can pass some values here I would pass the value of F I would say a value and this value will be assigned to m1 so we'll say self dot m1 is equal to value so you can use constrictor to pass the value or they can use setters right so we have get methods and we have set methods now four different variables will be having different get and set methods right so if you have three variables you might be using three get methods and three set methods and that's what they're called as getters and setters right so Gators get the values that you set the value get us only fetch the value it will not change the value that's why we say axis centers they change the value and that's why we say mutaters so this makes sense we're done again you can call these methods right but this works now once you talked about instance the second type we have is a class method now why we use class without see if you talk about this variables m1 m2 m3 those are your instance variable right an instance variable can be used with instance methods what about this one this is cool right now school is basically your class variable and if you want to work with class variable you need to use a class method and the way you do that let's say I want to know the info for students I will say def info info will print the name of this school okay so let's say if you are studying for the disco you'll be it will print any scope and of course all the students belong to same school right if you change cool name for one student it will affect all this for example this channel name earlier was having skills why don't we have change it to the disco so if the name changes for one subscriber it will affect all the subscribers right and that's what is happening here so if I want to print info and that to a school name I don't want marks here of course we don't want to work with self we want to work with clubs right so if you are walking with instance you will use a self keyword if you're working with a class variables you have to use class c LS okay it should be c LS it should not be something else now once you got that you can simply print the value so you can say plain or you can written the value your choice or we can say return c LS dot school that's how you do it so you have to say c LS dot school so any way you want to work with class variable you have to say c LS dot school okay but then how do you call info it's very simple you will say print s 1 dot info we can do that but there's only one problem in fact should work with all the objects but it is not specific to object so your self using s1 we can use a student thought info now this should work let's try I will say it one oh we got an error it says info missing one required potential argument which is oppositional argument which is CLS oh we had to pass CLS that's weird I don't want to pass CLS right in fact we are not even doing that for average we are not passing s10 so I don't want to pass thing normally what happens you know if you want to create a class method we need to use a special symbol or special way of doing that and we can use something called you take potatoes so we have to say at date and you have to mention this as a class method I can we'll talk about decoders later in detail but time being we got a class method decoded so if you want to use info as a class method you have to say act class method okay that's how you're defining so let's run this code now and it works you can see that we got the disco they're quite simple right so now we know about instance method because it works with instance variables now we know about class method because it works with class variables how about static methods say think about this let's say if you want any middle look it doesn't matter what method we want here if you want a method which has nothing to do with instance variable which has nothing to do with the class variables we want to do something extra okay so something different which is not concerned with the variables at that time you will be using a static method because we are not concerned about instance variable we are not concerned about class variables example let's say I want to print the info about this class okay not about student but the class so I will say def about you know in fact instead of saying info for class I would say get school name that will make more sense so I'm changing the name from info to get school for class method okay not for this and let me also change from here so we say get school here I will define a function and the function it would be info because when I say info I want to know the class name okay so this is not related to the object this is not related to class as I will keep it blank okay so if you don't want to read this to a class keep it blank if you don't want to relate this to object keep it blank no self no class okay static method and this will write down this will print in fact not return this will print we can do whatever you want so we can say this is student class who can do that right so we are printing this is student class and ABC module that's it you can print whatever you want as I mentioned important is this method is nothing to do with the class variables and the instance variable now where it will be useful so let's say if you want to perform any operation which has something to do with the other object of the class of objects we can use static methods here now if you want to perform some operations like finding a factor of a number right because the factorial has nothing to do with the class variables or instance variable maybe you're passing a value you just want to know what's a factor of the number you can use ascetic methods there now since we are using a static method we need to use a special decorator here as well so we have to say any guess we will be using static methods so for class because you have to say class method and for the static method you have to use direct method okay and then you can call it so you can say so you have to use class name so you would say children thought info right so this static so you don't have to pass anything not even class and not even object let's run this code and it works okay so basically we have different types of methods we have instance method which works with instance variable class method works with class variable and static works with nothing it has if you want to do something extra with a plus nothing to do with class variables or instance variables go with static now at this point we have talked about different types of variables and we have talked about different types of methods right so we can write variables and methods inside a class right so a class will have variables and methods the question is can we have a class inside a class that sounds weird right why would you want we'll have classes in a class so let's try to understand that so let's say I have a plus here and this plus 9 is to deny visually the way we have done earlier and we talked about student we have different types of variables right let me define that function first and the variables which I want here is self first I want and name of course in the last example we have taken box but here that sticks something different I would take a name of a person and that will be coming from a user of course or from Vindicator object with same name the second thing I want is roll number and I want these two details and then have a sign name here and there is a self dot roll number now we have used to it right so I would say the whole number right now once record these two variables and what I want is I want to print them the way you print this values is by defining some functions right of course you can do that directly a five grid object here so let's say the object is s1 and I was a student and when you create this object you have to pass values as well right the first one I want have is Naveen and rule number is related to the second object we want here is s2 and we'll say student in which you will be having two things of course I would say this is house Jenny and the roll number is let's say 3 so I have Naveen and we have Jenny here we call two and three okay so let's say I want to print I want to print s1 so I want to print all the details about s1 okay I want to print the name and roll number so that way you do that is by saying s1 dot name and we can say s1 dot roll number so this is how you can print the values right that middle and this quote and you can see it works recorded their details about s1 now what I want to do is I don't want to see this doesn't look good right what you should be saying it should say s1 dot show so when I say this will not show it should print all the details about s1 maybe it has so lumber name or different variables right so of course we need to define that function or method here so we'll set ever sure so this will print self dot name and will train itself dot roll number so we are pending this to data right it should work let's run this quote and you can see we got the same data now let's say student also has a laptop of course life when you say you are doing IT in fact we have done that right so you're a student of this Co you will be using a laptop to run your code or Dexter doesn't matter and let's say we want laptop here so of course I want to know which laptop we are using so I want to know your conflict now in that case I will also ask a user hey tell me bitch-slapped up your using the problem is we talk about laptop there different things important why it's not just about land you will not simply say here I have HP laptop I have a Lenovo laptop you will also mention the configuration may be CPU and one that's very important may be any i5 CPU with let's say 8gb RAM is enough so I want to know your conflict so what you will do when you pass three variables like you will pass a brand HP and then he will also pass the CPU which you are using let's say i-5 or you will also pass let's say Ram of 8gb yes you can do that ok so I have to accept this variables here I have to accept that here right and we can do that the other option which we have is to be different ok so the other option which we can do here is we can create a class ok we can get a separate class as laptop or issue of creating up glass outside this student we can also create that top inside student so a laptop will be only used by student and in this case seven surplus and as a laptop so we are creating a laptop class inside a student so that we can get that group okay so we have to also say def here and we have to say in it right and then let's create variables so the first variable we need here is brand and let's say the brand is HP that's the first thing we need second thing we need is the CPU which you are working with I would say CPU is I five and the third one is RAM let's say GB RAM so we got these three things right now we got these three variables and where to create the object of it so we can create the object here itself so we can create the object inside the init and normally that's what we do so if you want to create the object of laptop we should be creating that object inside the outer class okay so the object of laptop will be the inside the student class the way to that is very simple here you will say self dot la P you can create any variable you want and you will say laptop or we are getting a door so I'm going to say self dot laptop okay this works so you can see in the constructor itself we can define a variable so in the outer class you can define a variable lab and you can define the object so if any one you want to use a laptop you have to say this student object dot L AP example outside if I want to use it I will say s1 dot L a P dot whatever brand you want to fetch so you cannot simply sell laptop time you have to say s1 dot lab dot pland because the lab object is inside the student class this is one way what if you want to create another object of it let's say I don't want lab I want so you can simply say lap 1 is equal to s 1 dot lab you can get another object for s 2 you can say lab 2 is equal to s 2 dot lab because for every object you will get different type of object okay trust me this is how it works in fact you can also check that we got two different laptop object here we will print lab 1 and let's spin laptop so let's bring the IDS of lap one lap to record two different objects here so this works ok this is where you create in a + ok so we have a laptop as a in a class of a student right you could have done the outer class as well I mean you could have to do different classes that you want that works but sometimes when you know that this class will be used only for student noting else then you don't have to create a separate file for that you can do that in student class itself now can i data create the object outside let's say I don't a bit of laptop inside the student class can be do it outside directly let's try so I would say that one is equal to I will say student dot laptop now why shouldn't here because you cannot simply access laptop data right because laptop class belongs to a student class so we have to always say student dot laptop instructor you cannot simply create directly ok so this is one thing you have to remember you can create the object of inner class inside out of class or you can do that outside the outer class provided you are using the other class names you have to say student dot laptop ok this works in fact just to enhance the example what I will also do is we can create a show method for a laptop as well so we can say show for laptop and you can see we have two so methods one is student and one is laptop ok so in such students we have show method inside laptop also we have show but these are two different show methods the show method of student will print the data about a student and this show method of laptop will print the show method of student I know that makes it that's weird so let's do that so I will say print self dot brand self dot CPU and self dot run ok so we are printing this data so this show is different from this show let me now print laptop here let me on this quote on you can see it prints only the name and the roll number I also want to pin the laptop oh you read the object right we have removed that statement because if you want to access laptop you have to say itself dot lap is equal to laptop what you have to say itself so this is important time after printing this will call lap dot so because we also want to do that so you will say self dot laptop ok so we are printing data about a student and laptop as well so this should work let's run this code and it worked you can see we call data about student and laptop as well so this is how you create the class inside a class [Music] now in this video we'll talk about inheritance now we'll talk about a concept of oops you know way which is object-oriented programming we have so many concepts to talk about and one of them is inheritance now in real life what we have is we have this parent and child relationship right so whatever belongs to your parents belongs to you example if my father or mother has a phone it's my phone right by default my phone would be my phone but my father's phone would be my phone right that's how it works so if your parents house it's your house that's what we say inheritance right in the same concept you can implement in programming as well in fact all the languages which follows OUP's concept they do implement inheritance right and the way you do that is with the help of classes to us in hit a plus let's try that what I'm talking about is let's say if you have a class and class is a ok again it should be a logical name it so you can go with employee class you can go with student class you can go with laptop class whatever class you love but time being just for the example we are going for Class A and Class B or Class C in future we'll try to implement some other examples using different names and with the example but at this point just to keep it simple let's go for Class A now if you talk about this Class A this class they will have multiple features right maybe we can define two methods here so we'll say definition there the first method which I want to go for is let's say feature 1 ok so let's say we call this function which is feature 1 and this function does provide you something else okay so time when you are printing feature 1 working that's it nothing much of course it can be a complex code but make it simple it's to keep it that and then we can have another feature here let me just copy paste this code so I was a copy and paste and this would be my feature - so we got feature 1 and feature 2 and this will print feature 2 is working so this class a has two features we got feature one feature two right and the way you can access them is with the help of object right and the way you create not it is very simple you simply say a 1 is equal to a that's how you create the object right when a is your constructor again you can find that init method here but not needed at this point so this will work and using this a1 you can call feature one you can call feature two light so you can say a1 dot feature 1 and a 1 dot feature tool and this will work let me run this code and you can see record feature one working feature two working so this code is working now what I'm excited about is let's say we have one more class let's talk about Class B so it means create a Class B here itself so here I will say Class B and Class B will also have two features I will name this as feature 3 and feature 4 instead of typing them that means to copy paste the code here and we'll name this as feature 3 and feature form right see normally what happens you know when you work on a project of course we have a big team right so example if one person is defining a class okay in that class we provide you four features or four methods maybe you want to define some other class in which you need some extra features and you also want the existing features you don't have to redefine it right you can use it and the way you do that is simply saying example it in this class B we got two features right the got feature three which we got feature four and of course if I create the object of b1 so it's a d1 is equal to B and the functions which you can call using b1 so if I say B 1 dot you can see we got any two options we got feature 3 and feature full of course right because in B class we have only two features what if you want to get the features of a as well and that's where emittance comes into picture so we can say hey this B is a charge class of a right and the moment you say child classic will input all the features what I will say is this B is a child class or you can say subclass we have different terminology we can use here and we can simply say in the bracket you can say 8 so this plus B is inheriting all the features from a and record it right just when I think that one thing you know just bracket a we are saying B is inheriting the features from a which means with the object of B which is B 1 in this case the moment I say B 1 dot C the options you can access feature one feature two feature 3 and feature 4 and that's the importance of inheritance which simply means if you already have a class which provides you some feature and in future if you want to create your own classes in which you want to use those features you just need to inherit them right so we can use some technologies here we can say super class subclass or we can say parent class or a child class so time it will afford an superclasses a-plus so super is a class which is a and a class which inherits the class we will call them as subclass so B subclass and a superclass so that's the concept about inheritance now this is also called as a single level inheritance we have some other types as well we also have a multi-level inheritance now what it means example let's say if I have another class here let me get one no class here and this class is let's say C so to call this class C or this plus C inherits B now what will happen is what do we do in this class let's say time beam I will depend only one feature here we'll say feature 5 you can see how lazy I am so this is feature 5 working right so we can see class which has only one feature but the moment you create a object for C 1 so I will say C 1 is equal to C don't we that say C 1 dot C the methods you can use so we can use feature 1 now feature one feature 2 are coming from a right but C is only extending P so that solution should we have grandparent we have parent and then we have child right so a child can access all the features from parent grandparent great grandparent right so that's how it works okay this is working right so regards singles where you have one superclass one subclass then we have multi level then you have super class subclass and then aggressiveness of that superclass okay this is working what if you go for my tip pal what is my sequel here let's say the C is not only taking from a and B we got some other class as well just for time being what I will do is I would say B is not inheriting a okay so a and B there are two different classes nothing they are not related to each other a provides two features B provide two features okay so there is no relationship between a and B that to two different classes C says I want to access features from both from a and B so in this case you see will unite from a and B poor so you will say a comma B so C with copy from both or save a late-night from both so we will see one though me just see when you can see you can access all the features that's not the case would be one now b1 can only access to features feature 3 and feature four because b is not uniting a okay pointer member so we got a we got B and then we got C C is uniting both the classes a and B so this is called as by D pal so this is your by tip all right so we have done get single we have done get in level and then we have multi-party this is how we can work with inheritance nothing is fine we have talked about oops concept right and which we have seen inheritance right now what is inheritance if you already have some classes and if you want to use the existing features you will simply say class B which will take the features from a that so plus B inherits a or you can go with a concept of multiple inheritance where one class will extend from two different classes all more than two classes right so in this case we have Class A Class B and then we have plus C which is plenty which has a feature five but it is also inheriting a and B so we can say C has five features right so that makes sense now what do I do in this video is we'll talk about two topics the first one is the constructor in inheritance how it behaves and we will also talk about method resolution order all you can say mr oh so let's start the constructor time bin let's ignore C okay so let's say we don't have this C so please note that part and in fact we'll delete that part from for time bin and this focus only on a and B where the B is inheriting a okay so in total in B we have four features right now what happens is if I create an object of a so let's say if I say a1 is equal to constructor of a so this will work right so when you say a bracket that's a constructor it will call a method which is in it now even if it don't define it it is there somewhere right but let me define my own constructor on in it here so I will say def and will define the get method and this inhibitor will print one stuff so let me say print in a in it nothing much we are not doing anything extra here we are just printing in a in it that's it now what do you think what it will print of course we are creating object of a right so it will only execute a C with the object of a you cannot access features of B okay so even if I try example if I say a1 dot you can only access feature 1 and feature 2 but yes if you have an object of B you can access all the features right features from a and B because b is a subclass and a is superclass and as I mentioned subclass can access all the features from superclass but that's not wise right a superclass cannot access all the features of subclass not even one which are subclass ok this is fun now what if I create object of B so I'm not getting object of a hearing on stead of P doesn't matter what is a 1 or B one important is if I create an object B will call the constructor that's the question because we are not getting object of a they are carrying object of B we need Paul the constructor of a that's the question let me just run this code and it works ok so even if you have the object of B it will still call the constructor of a right that's how it works but what if you have your own constructor what if you have your own init method example is be seen be we don't have that in it right and that's why it's going up so let me repeat since we don't have it in it inside B that's why it is going to a but what if you already have in it with you so I will simply copy paste here in B so both have in it but then in this I'll print in B pin it right so in a via printing in a in it and B we are printing in be in it so this makes sense now wafer than this code what it will print it will it print in a in and it or in be in it of course right if you are carrying on it of B it should only call the init of B right and let's see if that works and let us run this code and if we alright so when you run the code you can see we only got in be in it so that means if you create an object of B first it will try to find the init of B if it is not then it will go for a okay that's the point you'd remember but what if you also want to call the idiot of is it possible I mean if I get opposite of B can I call the in it of both D classes I mean a and B C by default it will call only be right what if I were to call a as well and that's where we have a very special keyword or a method you can say and that is super so with the help of super so you can say super and it's of method basically so you will say super dot the moment you say super you can access all the features of the parent class so we can say super dot in it so what we are doing is we are trying to call init method of class a so the moment you say super you are representing this super class okay which in this case it is a so we are trying to call the init method of class a that's important now let's run this quote and you can see we got both the output we got in a and and we got in B and so point to remember is when you create object of B it will call the unit of B first and from the unit of B you are trying to call the unit of a so it will jump up it will execute the unit of a first which will print in a edit and then it will come back to print in B in it okay that's why we got the output so this makes sense so we can work with constructor in inheritance right now let me add a twist here so what I will do is I will remove this super from here let's clean it and I will not even say that this is B so Class A and Class B two different things okay they are not linked with each other but then we have Class C as we have done earlier will say Class C and Class C will inherit both the classes a and B okay and now if I say def and in it you know that what we are trying to do here first of all will print in C in it okay and then I will create object of seat so there's no object of a and B are 20 object of C now what you think will it call the edit of C or a or b of course right when you say the object is of C it will call the init of see only let's run this code and that's why we got the output as in C in it so this works perfectly but what if you want to call the init method of a superclass but there's a twist here now C has two super classes right one is a and B the moment you say super dot in it now what do you think which I need to it will call will it call it in it of A or B that's a question let you want this and let's see what happens I want me to send this on this quad you can see it says in a in it that means we are unfair here we are biased towards a we are not taking be here right this is completely wrong but the thing is we have a concept of em odd Oh which is method resolution order so what happens is whenever you have this multiple inheritance it will always starts from left to right which means so first so the moment you say in it it will try to find the in it of itself so since we have in it here it will execute the elite of C then the moment you say super dot in it now we have two classes like a and B and on the left hand side we have a and all the other side we have B so it will prefer left one first so it goes from left to right and that's something I've remember it's always from left to right okay and that's why we got in a inning the same thing can be done for methods example let's say we have two methods which are same so in this case you can see we have feature one in a and feature one in B both are the same methods with the same name of course and in this I will train some different message here I will print feature 1a and here in Frey I will say feature 1b that means the feature one is an A and feature one is B and now if I try to call so I will say a one dot feature one will it call from a or pit as a question and we know the answer right it will always call from a because it goes from left to right and you can see the output so there is something to remember which is a concept of method resolution order so basically in this video we have talked about three things the first one is how constructor behaves in enlightens the second one is how to use that super method and the third one is mro which is Method resolution order now in fact with the help of super method it can be called function let's see that so let's say from a we have a function here which is deaf and I will say feature feed that's it and in this I'm trying to call the method of super class right so we can say super dot and we can use that thing okay so you can say super dot feature - and let's say please working let me call feature it should call the feature - so yes it was so you can also use super method to call other methods as well not just in it so to represent this super class we use super method okay now we have started talking about objects right and we move towards glass and then we are discussed about in tight ends now it's time to focus on one of the major concept in ops which is volume autism oh that's a weird word right now we talk about polymorphism it's back down this word body and morphism so poly means many right and when you say more it means forms so that means one thing can take multiple forms example like as human you know we have different forms as the situation changes we change ourselves right example we behave differently different location at office we behave differently when we are with our friends we behave differently so we are polymorphic in the same way we talk about object-oriented objects shall have multiple forms so this concept is very important when you talk about software development so we use this concept a lot when it comes to loose coupling dependency injection we also have a concept of interfaces which was later the four ways of implementing polymorphism the first one is duck typing in Python the second one is operator overloading then we have method overloading and method overriding in the next few videos we are going to talk about all these topics okay and one of the interesting one is that typing especially if you are coming from different background let's say if you have already seen Java or C++ or C sharp this is been felt for you because in Java or in seiza we have to mention the type of the variable which you work with why do you have to mention the type of object you work with but Python we have something different right if the last video we have talked about polymorphism right which simply means you have one thing which will behave in a different way or you can say one thing and multiple forms I that has many forms now in this video we'll talk about the first way of doing that and that is you're done typing I know that's a weird void right typing we have this famous line which is if there's a bird which is walking like a duck which is quacking like a duck and which is swimming like a duck that bird is a duck right which simply means doesn't matter if it's a duck or not what matters is the behavior of that bird if it is matching with duck that's a duck right now how did you implement that in programming so what we will do here is let's take an example let's say if I have X equal to 5 now we talk about it type now in Python we have a concept of dynamic typing which simply means the type you can mention later example when you say X equal to 5 the type which we are representing now is integer but what if you say X equal to let's say if Naveen I'll be changing the type of X Y so that's not the case what is happening here is when you say 5 in your memory you've got a space which is of type integer when you say Naveen in your memory you got a space which is of type string the X is just a name to it okay so when you say X equal to 5 there is an object of type integer you are just naming it as X later when you say you got Naveen you've got some space in your memory and you are represent that with X X is just a name to it okay so we don't have specific type 2 X the moment you say type of X you're actually getting the type of 5 when you set the type of X you are getting the type of Naveen right so that's one thing at remember the moment you give a variable name that's just a name to our memory not understand duck typing what we'll do is we'll take another example to extend this what I will do is I make it a class and we'll leave this class as laptop and this laptop class will have a method which is let's say quote so in this code what I will do is I want to execute my code right so as a programmer what we do is we write code right we write code and we compile it we run it and we get the output at the end but then to do that we also need an i d-- which is integrated development environment and in this case if you wanted a code you need to pass an IDE so we are expecting in the arguments that someone will pass an IDE to us and using this IDE I will say IDE dot execute so the question is the IDE is of what type is it a type of integer is a type of float is a type of string now of course right when you say execute that means this is something which is not dead in the existing classes which you have that means the type of IDE is something very using it is something which is user is defining right that means if you want to create this object IDE you to create an own class let me get a class here and I will call this class as pycharm now for different languages we use different IDs in fact for one language we have multiple options right example when you work on Java maybe you'll be using NetBeans Eclipse IntelliJ for Python as well we have different ideas we are using pycharm here because it works well now when you say Pi Chun of course it will have a method right so let me define a method which is def execute so you can see we have this two statement the moment you say execute it will say compiling and running that's what you do right where your code gets compiled and your code get run now will this work of course not because we are not calling anything so when you run this code you will not get any output let me just write out I would right click and say learn them or you can see there is no output because we are not calling methods let me call code the way you call code is by creating the object of laptop first because you cannot call code without laptop so I will say lap 1 is equal to laptop and with the help of lap 1 now you can call code so you can say lap 1 dot code but there's one problem here which is inside code you have to pass an argument which is of IDE that's a question now how do you pass an IDE here so what I'm going to do is first you need to create an object of IDE he will say ID of course you can have different names here but let me go with IDE so IDE equal to this will be of type PyCharm so the type of IDE we are working with this pycharm right which is user defined of course and then when you are calling code you have to pass IDE and now it should work you can see from this code your quad compiling and running so the type of IDE here is by charm but is it fixed can we change the type of IDE later let's say we have one your ID let's say in future I'm putting my own editor okay we just we're better than pie chump maybe and in that as well we have the same method let's say execute and in that I'm doing some extra stuff you know so my IDE also does spell check maybe by jump does that my IDE also does what convention checks you know the name of the variable and different stuff and then it also does compiling and running right so my IDE does extra stuff now what if I want to change the type of ID is the ID type fixed to PyCharm not exactly because this is dynamic typing right so you can replace this IDE type from pycharm to editor provided you have that method which is execute right it doesn't matter which class object you're passing what matters is that object should have the execute method right because in ID we are saying execute so now even if you change from my charm to my editor there is no problem the code will still work like provided you have that method okay so if you should be having this method is execute and that's the case if there's a bird and if that bird behaves like a duck it walks like a duck it quacks like a duck it swims like a duck it should be like duck in the same way if there is an object which is IDE and it has a method execute that said we are not concerned about which class object it is what we are concerned about it should have that method which is execute and that is called as duck typing right I know it is amazing and if you are coming from different background like Java so we have a concept of interfaces right so that's what we do we create interface and we have this my editor and pycharm as a class which will implement that interface but if you are new to this programming don't worry that's our path right this is simple we have started talking about polymorphism right and in that we have seen one example of duck typing now in this video we'll talk about operator overloading so what is operator overloading so we know a concept of operators right so we have to offer and you want to add two numbers you can say 5 plus 6 well 5 & 6 are your operands and plus is the operator so we know that right and then we have a concept of polymorphism so it simply means you have one thing which has multiple forms right now if you can think about this we have two integers and then we are trying to add them we can also add an integer and a float right so in different programming language it does support what about two strings so if you say you have two strings let's say hello plus world will it work and there's this yes right so plus works with the strings as well but what if you want to say you have a number five so let's say we've got a and a is five right and then we got B and B is let's say void now can I print by saying a plus B the moment you try to underscored you can see we caught an error it says unsupported offering types 4 plus so we cannot use int and string 4 plus right so all these things are pretty fine all these things they are called as a synthetic sugar which simply means it is trying to simplify the code for the user see behind the same things a bit different imagine this one example if I say a is 5 and B is 6 now what do you think what is happening behind the scene now trust me whatever happens in Python happens with the help of object right and here as well may talk about a and B the type of it is this ain't right so int is a class here and when you say class of course class will have certain methods right so behind-the-scene what is happening is when you say a plus B which is of type integer it is calling something so behind this it is calling ain't dot the moment you say int dot int is a class right and you can see we have a method called s in it we have a circle as ABS and we also with a call s ad which is very important here domain say ain't dot add this is taking two parameters okay the first one is a comma B so what we are doing here in print a plus B the same thing can be done here the question on this code here you can see we call the same outputs of both our printing 11 so you can say a plus B or you can say ink dot add by passing two parameters a comma B and of course the first one looks cool right you're simply saying a plus B because from our childhood we are doing that we are trying to add numbers using plus operator but I'm going to come to programming in programming whatever you want to do you will be doing that with the help of methods and as a method which belongs to the int class right if I jump to integer so you can press on the control button and click on the method which you want to see on the class which you want to see you can see it's a class and if this class has multiple methods in the same way the moment you say ain't dot you can access a method call this ad and you're calling a Palma B so even if you say a plus B behind-the-scene this is getting called right so this is one thing get remember okay so we'll talk about operator overloading later but this is something you have to remember so whenever you add two numbers this is what is getting called behind the scene now if these two things are string so if I make it string here now this is not integers right there string so I have to say STR dot add now when you say steer that and in fact STL also has a add method which takes two parameters and both should be of the same type in a string type and then it will work right so from this code and you can see it is working so we got five and six and a string so they caught point catenate adhere so this works the moment you change the type of it will not work because the embedded class doesn't have two things which is integer and string together right so this thing it remember now once we know that moment you add a plus operator it calls the add method the moment you put a minus operator it will call a sub method the moment you use a star simple which is multiplication it will call mult method so we have different methods for different operators right and normally those things are called as magic methods but that's what they say magic methods right so all these operators behind the scene they work as methods not to understand the concept of operator overloading what I will do is I will say class and let's say we have a student class and every student will have let say two variables and that will be marks one and marks true just to keep it simple so whatever here is all set deaf and in it and here I will say self dot m1 is equal to PO now we want the value from the user right so or maybe I want to pass the value so I will pass it from here m1 comma and 2 and let's specify self dot and 2 is equal to m2 so we got these two values right now what I want to do is I want to create two objects ok tutions object or one is let's say s 1 is equal to student and this will take two values I would say 58 comma 69 that's the value here and then s 2 is equal to a student and this student will have let's say 60 comma 65 so we called two marks here right we got two student and a student have two marks now I want to apply the operator here which is plus operator so I want to add this two students so the moment I say s1 plus s2 I want a different student object right is it possible to do that let's try so I will say s 3 is equal to s1 plus s2 because now we know that plus operator means it will add two values but it is possible with the help of integer it is possible with help of string is it possible to use plus operator with student class that's a question so that's one this for let's see what happens okay we are not printing the value of s3 but let's run this code the moment you try it when this code you will get an error oh okay we got different error here it says student takes no argument but it is taking arguments right what's wrong oh we have it on the wrong method name my bad let's run this code once again so I was expecting this error not the first one and the other is it says unsupported operating type plus we cannot use plus between student and student because we have not defined it right if you remember behind the scene even if you use plus with integers it will be calling a add method but if you see our class we don't have that add method right because if you say s1 plus s2 how your compiler how your Python will know what to do and that's where you have to define it yeah that's we have to say hey the moment if anyone says plus of a student you need to call this method call has ADD okay so you can overload the operator and you can change the finishing for it you can you find anything you want right so I can say add and this ad will take two parameters the first one is self comma other and you can see week takes two parameters itself and okay guess what is happening so behind the scene this code is getting converted into it is student dot add which takes two parameters s 1 comma s 2 so what itself and what is added here so the first parameter is self and s 2 is the other parameter you can change the variable name that's fine okay you can say this is oh and it should work it's a variable name right so this is what is getting called behind the scene right so here what I will do is I will say let's take two variables m 1 M 1 is equal to self dot M 1 and M 2 is equal to self dot M 2 so we've got two variables right oh not just self m1 plus it is other dot M 1 plus father dot M 2 so this is how you add values right so self m1 and other M 1 and then n 2 will have the addition of self m2 and other M 2 so this works and once you've got these two values what you'll be doing is you'll be getting a new student object will call the rs3 you can have any name is a matter so s Li is equal to a student by passing these two values M 1 comma M 2 and now once you've got this student you will return as 3 here right that is what you're expecting so the moment you say s1 plus s2 it will return the value it will return a new object of student and you'll assign that to s3 ok so this one will happen now if I print the value for x 3 so I will let me print s3 dot m 1 and the moment we have this code run and you can see it works we've got 1 1 8 is it right of course it should be right so it is 58 plus 60 and that's right we got 1 1 8 this thing is working right perfect so if you want to add two students you need to overload the operator of plus because integer knows what is plus string knows what is plus your student class don't know what is that plus means so plus means call the add method but we don't have an ADD method here so we have to define our own method the same thing can be done with subtraction is - the same thing can be done with multiplication so I would recommend you to explore more on this you know so in the moment you say def underscore underscore you can see we have so many methods we also have greater than we also have equal to symbol we can compare two objects we can say the different methods available ok so try it out we also have multiplication somewhere so you can see we also so everything is predefined you can just use them in fact for this example I will take one more I want to compare if two objects are greater than or equal to example if I say if s 1 is greater than s 2 this is the horrible way of comparing students because marks is not a criteria where you can Compassion's but just for the example I would say s 1 win so whoever has maximum marks I will say they've been I would say else branch s 2 wins so what they're trying to do here is we one has maximum mass they will win but when you say maximum marks how do you check it because we are not defining it right maybe I want to check when the first marks which is M 1 or maybe I want to check and do all maybe the addition of both you can try it out so what I'm going to do here is this will work of course not because the moment you try to on this code it will give you an error by saying together then symbol not supported between two instances of student that is supported for integers because it is pretty fine so here as well if you want to make it work you have to define a function of method which is GT GT is greater than GE is greater than equal to so you want to check when the greater than and then here you'll be using self and other as usual because behind this thing that is what is happening it is saying student dot GT and it's passing two variables s1 s2 so it's one goes to self as two goes to other and now how do I compare soon as the s1 doesn't matter is it as one of something so we say s 1 is equal to self dot M 1 plus self dot M 2 and we'll say s 2 is equal to path o dot M 1 plus other dot M 2 so we are adding the mouse basically you can compare if s 1 is greater than H 2 now this time s 1 s 2 not objects ok they are simple variables of integer ok if I get in confused with this let me just make it our that should make sense so let's compare R 1 and R 2 now those are just marks right if R 1 is greater than R 2 we will say return true else return false simple now if you know this code we are comparing it right let's compare and let's run and it worked you can see s 1 beans right because s 1 values are higher so 58 plus 69 is higher than 60 plus 65 but what if I change the value for s 2 let's say this and you can see of course now s2 is because let's run this code and you can see it's a that's two wins so that's right so what you are doing is we are adding those values and then we are comparing it so if you want to perform any operation on the objects which I usually find you have to define all these methods okay there's one more what if you have a video but let's say a is equal to nine let me to say print a and you can see it will print the value of a there's nothing wrong with that you can see it is printing the value of a no is it printing the address of it that's not the case right is not printing the address of a it is printing the value of a but what happens the moment you try to print s1 it will not print the values of s 1 it will try to print the address of s1 ok so you can see that it says it's a student object at the address at this position hey we don't want address right we want values what is happening so when you try to print the object doesn't matter is it integer or your class behind the scene it is calling our method called as STR it is happening behind the scene okay even if you don't call it it is happening behind the scenes so moment you say print a it will try to call a dot STR and that's why you are getting the output right you are getting 9 because it is calling STR in the same way the moment you say s1 even this is calling SD r right and now from this code you can see two still giving the same output that means it is calling STR what is this STR so if I click on this you can see it is inbuilt end so even if you don't define STR function or method in your own class it is getting defined somewhere and that definition what is printing it is printing the module name it is printing the name of the class and it is printing the object address we don't want that we want values right that means if we need to override this method so what I will do is I will say def and the method name is STR so you have to define this by yourself so we want to return the value of M 1 and n 2 right so even say self dot M 1 comma self dot and 2 and now if I want this what it will be done a couple of course let's run this code and you call it connects alive we got 58 and 69 so now if you want to print the object it will not print the address it will print the value right because we are overriding the STR so if you remove STR they fit on this quad you've got an error so it is ruining a non string value we have to return a string value how do we do that because by default when you say print you want to print a string right so what you will do is you will use a format here so you can say cut it back it's cut it back that you are printing to stuff dot format and inside this format you can pass this to value so what will happen is those curly packets will be replaced by these values so you are returning a string now and we have seen that before right so let's run this code and it was you can see quad 58 and 69 so even if you say print s1 it does work you can also print s 2 if you want and let's run this code and you can see we call 69 65 so that's perfect so this is how you work with operator overloading ok so point remember is whenever you perform any operator like addition subtraction deletion behind-the-scene we are calling methods ok so forth plus we use add method for - we use sub method and for star we use multi-method in fact we have list of methods there in the last video we have talked about operator overloading right which simply means you have or operator like plus minors or division or multiplication so the operator will remain same but the operands will change why the type of parameters we are passing it will change example when you say plus we are calling the add method right and Agnetha takes different types of parameters or different types of arguments so that is overloading so it simply means you have the same method name but the arguments are different or may be the number of arguments or the type of arguments are different in the same way we have two more types in polymorphism one is method overloading and method overriding so what it means let's talk the first one which is method overloading now languages like Java C sharp or any other oops language they have this concept of method overloading which is not there in Python but what exactly it is so it simply means if you have a class and in that class if you have let's say two methods with the same name but different parameters or arguments which is called as method overloading example let's say if you have student class and in student class if you have two methods let's say average so we have two methods with the same name average one takes two parameters one takes three parameters this is called as method overloading but in Pilot we don't have this concept so we cannot create two methods with the same name the next one is method overriding which simply means you have two methods with the same name and same number of parameters or arguments okay so that means technically two methods are the same name in same parameter in the same class of course not not a same class but let's say if you have a concept of inheritance we have Class A and Class B and both the class have the same method with the same name same parameter this is called as overriding again we see the impact of that let's start with the method overloading first so if you want to achieve method overloading what I will do is I will just remove this methods from here just to keep it simple right so we got a unique method and it is working perfectly let's remove all the extra stuff from here we just want to create one object not even two I want to add two numbers so I have to create a method let's say some and some will take two arguments will say a and B so I want to add two values that's it nothing complex I want to add two numbers now if you want to add two numbers whatever you do is I will say s is equal to a plus B and at the end we'll return s that's what we want to do here we would just want to return the value of s okay and if you want to work with that you will say s1 in fact you will print the value you will say s1 dot sum and you want to add two numbers here I would say five and nine and let's see if it is working let's run this code and it was you can see we got 14 so yes we can create a method inside a class which takes two parameters and we can pass two parameters it works what if if you want to pass three values they want to pass five nine and six now this is not possible right the moment you learn this code you will get an error because you are passing three parameters or you're passing three arguments and you're accepting only two so this will not work that means you need to create another method Palace sum which will take three arguments that's what we do in other languages but here will not do that here what you can do is you can also use third variable you will say see you got a B and C and you will simply say a plus B plus C right so this will work so if I'm this cold and you can see you've got 20 and this is right you will get 20 so we are passing three arguments and we are accepting three arguments it would work but what if I'm not passing third argument impossible two arguments and now the person starts right because you are expected to pass the third argument as well how do we solve this to solve this thing we can use a concept where you will say a is equal to none so this is one options other options we can use the variable than the arguments if you remember we have done that before this star and variable name but this isn't the option you can say all the values are by default none which means even if you don't pass them indeed this is default arguments right so even if you don't pass the value the default value will be none so that means even if you don't pass any value example even if you don't pass five and nine it will work all the values will become none I want to pass some values I would say five comma nine I'm passing with two badges right let's start the third one let's say I want to pass three values five comma nine comma six all these values will be assigned to a B and C respected right so if I will go to a 9-mil go to B and 6 will go to C so this values will be plays by none so none will be replaced by this values right so now once I know this so we can simply add it but what if you're passing 2 so in this case you will check so before adding you will check you will check if it is not equal to none B is not equal to none and C is not equal to none so if none of them are none that simply means you're passing to the arguments right and if you're passing three arguments this is the calculation we are to go for you will say s is equal to a plus B plus C and you want to declare as outside so that you can use it from anywhere so you will say s is value point zero and you've got this otherwise if let's say if I'm possibly two arguments I'm not passing the third argument in that case it will go to LF and inside L if I will check I forgot the colon here so instead and if I will check if a is not equal to none so I'm concerned about a and B if they are not none in that case I will say s is equal to a plus B because we're not concerned about C right and then we can go for one more conditions what if you pass only one parameter or one argument in that case it will say else and he will say s equal to a so when you say only one parameter let's say if I'm not passing 996 and opossum only five so it will be 25 right that's what we want so we can do that else but it's not required here but just to make it more effective we can't pass one argument we can pass two arguments we can pass three arguments that's perfect let's run this code and it worked you can see we got 20 now if I pass only 5 and 9 even this will work because if you pass only 5 and 9 C will get none and it will execute LF and you can see record 14 now if you pass only one value let's say 5 in this case if you have this code you call it fine right that's how it works so this is your method power loading so we are overloading methods but then we are not doing directly because it doesn't support in Python so we are doing some trick the next one is method or driving so let's go with that let me remove this code and let's once again take a simple example and this time we'll go for a and B I love these classes so what I will do is let's say we have a plus a ok and in this class a we have a function is a function name is or the method name is show and in this method I will simply print in a show now first of all before going ahead this concept is metal while writing this is very famous in software industry you know so in future videos we'll also talk about this thing when we talk about interfaces and abstract classes this concept is used very heavily okay so make sure that you understand this concept property may be with simple example like a and B but understanding the concept is important here we can go with complex example as well concept is important so let's say we got Class A which has a mathematician show and we are printing in a show that is nothing fancy now if I create an object of a here so if I go back and if I say a1 is equal to P so we are creating content of a right and with the help of this we can Paul show if you know this code you can see we got in a show that perfectly work now what if you create a new class let's say Class B okay and now imagine this plus a is a parent and Class B is a child example let's say this is my dad and this means so we have B here and time mean I'm not writing anything here we say pass I just want to keep it empty okay so we are keeping it empty let me create the object of B not a but let me get object of B and let's run this code and you can see we got and either it says B object has no attribute as sure of course right and B we don't have anything at this point you will use a concept of inheritance and you will say hey B inherits a so when you say B inherits a it means you will get all the features of A to B and let's run this code and you can see we got in a show because of course alright and B we don't have show the permitted on this code it will first search for the method so inside B now since we don't have that it will go to a to search it and that's how it works so before going ahead let me tell you one story a small story I've used this story in multiple example when I was teaching Java as well so the concept is when I was in my leverage standard when I have just joined my college I was not having any phone ok so a lot of people they used to ask me which one you have so I should say I don't have any phone ok because I was not having a phone but then this question got repeated multiple times then I realize ok I have to say something now at that point my father was having of one which is Nokia double one double zero I was not having any phone so whenever someone is to ask me which one you have I should say I got Nokia double you know I don't have a phone right but my father's has a phone so far this one is my phone that's how I think works right so since B & B my father's a B in Heights a so I got all the features in this case show is in a doe I mean D don't have show after some time you know after few months I got a new phone and that is Motorola phone so my father's hasn't Nokia phone and I got Motorola phone now if you ask me which one I have of course I would say Motorola right I would not say Nokia right because my phone were lights my father's phone that's what has happened here the moment you create a show method inside B as well and if you try to print in be show because it when we were not having show inside B it was going to a now since we have show inside B it will train this show off be right so if you are say which one I have now so I will say Motorola not Nokia right so my phone authorites my father's one in the same way this show alrights the onion show method so this is how you work with method overriding again we'll be doing this concept more in the next tutorials when you talked about extra classes or interfaces but this is awesome right the name of this point when you call show it will call this show method of this subclass if you have it okay so that's it everyone I hope you enjoyed this session written in the comment section and do subscribe for for the videos