Tutorial 21- Python OOPS Tutorial- Classes, Variables, Methods and Objects


Hello all my name is Krishna come welcome to my youtube channel today in this particular video we'll start the oops tutorial in Python where we will be discussing about classes inheritance how to create static methods how to implement inheritance with the help of Python how to create instances inside the class and we'll also discuss about private variables public variable and protected variables also which is basically just kind of data abstraction we will also try to see what is encapsulation in Python how to do it with the help of oops concept so there whole lot of things that is going to come into this particular tutorial and we'll discuss in depth itself so let us go ahead and try to understand what exactly is class so the first question is what is class now you can definitely define as a class as is something like a real world object now if I take an example of car car is one a real kind of a real world object in short and inside car you have various attributes like you have windows you have those like these are basically an attributes and we can also define some of the functions inside a car some of the functions are like you use car for driving purpose right and each and every driving like when you are driving that particular car they are different different speeds that you'll basically be noticing in car ok like if you if you select some car like adi and BMW right they will basically be having high speed with pretty much good smarter features so in short if I just talk about class they are a real-world object which has some properties attributes and functions so we'll try to see how we can actually define a class with the help of Python so first of all in order to define the class with the help of Python you have to use a keyword college class let me just define class as one car okay and inside this I want to define some of the properties but initially before defining any properties I am going to make this class as an empty class so for making this particular class as empty plus I'll just write pause that basically means I don't have any properties defined yet inside this particular class and initially I'm just showing you one bad way of creating a class and later on we'll try to move towards writing in a proper manner like how should we actually define a class and how should we define all the properties and functions sited so let us go ahead now in the next statement what I do is that we can initialize an object from this particular class non object so in short if I just talk about this particular class car it is basically a blueprint right so instances can be created from this particular class so if I just consider one car as one class you can have any number of classes which can be derived from that particular class which can be instantiated from that particular class I think I should not say derive but instead I should say instantiate it so let me just show you how to instantiate from that particular class so first of all I'll say car one and here for instantiate I'll just use the same plus and this is basically just like a constructor okay so if you are from other programming background like dotnet and jar you basically write like this one so once I execute it okay it is saying that class is not a force on it should not be class here I'm going to write it as car okay so car with open and close braces now I'll execute it if I just go and execute car one you can see that it is basically a a car object is basically created at this particular memory location now if I want to define some properties suppose I want to define like this so I'm going to say that car one dot windows I'll just say that how many windows it may probably have I'm going to initialize it to five okay so I may also say that it has another properties like how many number of doors it has right so here you know that for a particular car it has for those I and I'm not just considering the back part but instead how many doors are present in that particular car now when I execute this I have created this particular instance now if I go and see the car one dot windows right so here you will be able to see that it'll just give you the option that how many because it has it has somewhere on five like this in the similar way you can create any number of objects so I can say car two of car now this is an another instance okay and this instance is not similar to this instance over here again you can define car two dot windows suppose this particular car has three windows suppose I'm just taking as an example this car just has two doors right so some like this now if I go and print this particular car information now so here you can see that car door windows it is showing you three now remember that is over here when I'm creating this properties away it may be that if I use this particular approach some person may also create another property inside this so suppose I say car to dot suppose engine time so here I'll say the engine type respect role suppose I'm just saying like that okay so I'll just write it as engine time and I'll execute it now you can see a way if I print it right and if I write car to dot engine type you can see that it is showing me as petrol now it may be happening in such a way that if I have not fixed the number of attributes right any any number of attributes can be created by the developer so this is a very bad approach now in order to make this approach right what we do is that now I'll just go back to my car class now instead of just using us right there is a very good inbuilt function inside class and how do you see that inbuilt function suppose here is my carbon right if I like dir of car 1 and if I execute it here you will be able to see one very good inbuilt function which is called as underscore underscore in it under squander scope now this particular init function is basically act it acts like a constructor you know so if you are from other programming backgrounds so you probably may be knowing what is that constructor so constructor basically gets used in order to initialize how many number of properties are getting used inside that particular class ok so what I do is that I will define this particular I'll call this particular initializer or I'll say it is constructor so I'm calling this and here inside this first of all you will be using one self parameter now this self parameter is plays a very very important role case I'll just tell you in a while what exact role it plays now what you have to do is that while you are initializing the object for object particular car like how you're initializing over here it is always possible that you need provide how many parameters or how many attributes it is good to consider inside that particular car so here I can say that okay window is one attribute okay door is one attribute and engine type is one a trick suppose I've considered three parameters now inside this I will start creating my own variables that is the attributes inside that particular car so in order to do that I will be using this self attitude the self is basically getting or it is considering this particular object it is basically directing to that particular object it is saying that for this object right for this object we are going to initialize for one parameter or an attribute what value it should have.

I hope it is making you understand but let's see an example over here now I will write self dot write self dot I'll say that okay my first attribute is my window so I'll say windows is equal to this particular window value which I am assigning to it yes then what I'll do is that I will go to my next line I'll say self dot door is equal to door okay and I'll make it as doors so this is the attributes that is present inside this particular object then after that I will just write self dot engine type is equal to engine type now once I execute it you can see that it is not executed perfectly now we have some information over here first of all we have defined a class called as car and inside the init function it is basically creating three attributes one is windows doors and engine type now if I want to initialize this variable again how if I know to initialize an object on that particular car class first of all if I just execute Chris like this right it will give us this kind of error it will say that in it missing three required positional arguments window door and engine so in order to do this what I'm going to do I'm going to provide the parameters like 4 comma 5 comma engine type suppose I say it as patrol okay and guys I'm just taking an example you can consider that engine type need to be patrol or it may can be of various versions also so once I execute it now you will be able to see that it has got executed successfully okay now the most important thing what is happening as soon as I called this particular class now you should understand guys over here as soon as I called this particular class by default this init method is called because this is my constructor this will initialize the variable or the attributes that I have defined over here and what value it should have it will take from this particular parameters now here you can see that when I write car okay and I provide the parameter there 4 comma 5 comma control right this is getting initialized for this particular variable so window is basically having 4 door is basically having the value as 5 engine type is basically having the petrol version patrol a string now what about this particular self now this self is basically getting initialized for this particular object so this self take the reference of this particular object and for this object the windows our tribute will be set to this particular value that door attribute will be set to this particular value and the engine type will be set to this particular value and this is what I am actually explaining about now as soon as I created this particular instance let me just create another instance saying as cause and Here I am going to say that it is 3 comma 4 comma bizzle suppose I say it has a decent okay now once I execute this right so this has got executed successfully if you see over here now what I'll go and do I'll just create a cell below and now let me write it a sprint car 1 dot windows now you can see that it I'm calling this attribute right so as soon as this is getting executed you can see that the car 1 windows is actually full that will get printed so once I execute it I'll just go up you can see the output is basically for right now similarly you can do a whole lot of things with respect to this we can also see different different features that are of this one different attributes that are basic if is it so if I write car a car to dot or for the car to I have actually selected a doors s4 so let me go and print this particular bat now you can see that I'm still getting four okay let me just print the engine type if you are interested in to that also so here you can basically see that the engine type is actually right so this is how you actually define an initialization constructor and this is pretty much simple and easy now let me just show you one more thing I can also create one method saying that what suppose I want to understand that what is the engine type from this particular method suppose I write it over here as saying as define type underscore engine suppose this is my method or just let me just show you that I'll say that okay one of the method of the car is like self driving okay so self driving is one of the method of the class so I'll say that again the parameter is over here will be given as self the reason I am giving it as self because if I give self over here I'll be able to access this attributes that I have initialized in the initialization constructor so I'll say self and then I'll say it as engine type okay now in the self driving it will just return some message saying as this is a this is a petrol or diesel car and I'll use dot format and I'll replace this placeholder with self dot engine type okay so let me just remove this I don't want to provide this parameter because I will not require it in sure okay so this is what this particular function is doing and this function is present inside this particular car class right so as soon as I execute this I'll execute car one and let me go below and just call car one so car one dot self underscore trial now if I call self underscore driving one thing that you usually should happen is that it should return this particular message saying that this is a patrol car right and over here you can see that it is taking self right now self will have the value of the engine type from this particular object because this self is nothing but it is a reference to this particular object so when I execute this over here you can see that it is showing me oh okay so I have to call a method sorry so here you can see that it is giving me a message saying that this is a patrol car pretty much simple guys you can understand that these are my properties right these are properties inside my car this is the method inside my car now this car can be inherited by an odd ECAR BMW car.

You know maruti car or any kind of car that you want and i hope you've got the basic idea of how to create and this is a very good way of creating advanced guys and a most in the real world IT industry also everybody uses this technique where they'll be creating a visualization constructor they well they'll be using different different inbuilt functions to basically define all the classes and the main use of is that guys you will be able to reuse this class many number of times and different different purposely.

It helps you to do the reusability technique that we usually require when we are writing some real words right so this was some basic understanding about the hoax tutorial with respect to classes in my next video.

I'll be teaching you more about the inheritance and different concepts and poops that is object-oriented programming language so I hope you like this particular video please do subscribe the channel if you have not already subscribe as you know in the next video have a great day everyone.