 Right, so hey guys and welcome back to another Python tutorial. So this video is going to be a bit different to something we'd usually do because in this tutorial we're going to step up the game a bit and we're going to learn about object oriented programming. So this is quite a popular approach of programming that is used by a lot of developers and it's a pretty crucial thing to know. So if you know this, it's going to be quite valuable for future jobs as well as just using it in your code because it really helps maintain code and there's a bunch of useful features that you wouldn't really have when you're programming your normal way. So without further ado, let's begin. So here are some of the concepts that are going to be covered in today's tutorial. I'm going to go through with you guys what object oriented programming is, how it is useful, what are classes, what is inheritance, and then we're just going to add some code just to cover all of it. So first of all, what I'm going to do is I'm going to create a new file, call it .op for beginners.py. Bear in mind, this tutorial is really aimed for beginners. So if you guys already know a bit about object oriented programming and if you're looking for something a bit more advanced then I wouldn't recommend this tutorial because it's for proper, proper beginners. So what I'm going to do is try and... Okay, so first of all, I'm going to copy these notes, stick them in my Python file right here, and then I'm going to comment them out. Right, so first of all, you guys might be wondering what object oriented programming is. So it's just an approach to a method of programming which is used and is quite popular amongst a lot of people where the main focus is an object to which you can assign data. And the data in the object can be in the form of methods and properties. So properties are like variables and methods are like functions in a normal code. So keep that in mind. Properties, every time you refer to that in an object, is almost like a variable. It stores data and methods is almost like a function in real world programming, but these are just terms that are changed when you assign a variable or a function inside an object. They turn into a property and a method. So I'm going to make a note of that. A method is a function and property is like a variable and stores data. Right, so that's a bit about what object oriented programming is and some of the common terms that are used. Now, we are going to jump into how it is useful and what really is the point of using it. So when you're using object oriented programming, code is a lot more manageable. You can split it into smaller sections and restrict certain functions from accessing specific data or you can also share data between functions. So I was going to sound a bit confusing for now, but this is just a bit of theory before we actually get into the concept. Once I start coding, you'll get pretty used to it. So this is another advantage of it. Another one is you wouldn't have repeating variable names. So helps cut down recurring variables. So if you had to make a change in your code, you're most likely going to have to make it in one place instead of having to make that change in pretty much the whole program. So I think these are enough of an incentive. Another really important one is if you have an object, you can inherit its properties into another one so that you do not have to copy and paste this data. So you can inherit properties, which is like a variable and method from another object if needed without repeating the code. So that kind of saves you hustle as well. So now what are classes? This is when we're kind of getting into the actual concept. So classes are a blueprint of objects. So you can also say they are used to create objects. So without classes, you can't really create objects. So keep in mind that classes are like the beginning or like a blueprint of an object. So it's like a skeleton of an object. And they're used to create an object. Without a class, you can't really create an object. So keep that in mind. It's really important. So now it comes to what is inheritance? I mean, it's pretty basic and it's self-explanatory. You can use inheritance to inherit properties and methods from another class. Now we're saying from another class because when you inherit, you inherit it into a class. And then you need to actually initialize the class into an object, which is when it turns into an object. So object is the future tense of a class. So a class actually needs to be, an instance of a class needs to be created in terms of creating an object. So this might sound a bit confusing, but just try and keep these points in mind for now. And now finally, the bit that you guys have been waiting on, let's get coding. So I'm quickly going to be erasing all of this. So if you guys want to take a note of it, quickly do so. And here it goes. Right, so now let's start coding. So first of all, as I said before, if anyone want to create an object, we need to start with a class first. So we need to have a class in order to create an object. So I'm going to give you guys an idea of what an object would look like. So let's say we had a car, right? The car would be our object. So the name of our class would be car, because obviously a class needs to be created before an object. And then the car would have methods such as, let's say reverse, which is going to be a function. That's a method, as I said before, and it would have forward another function or a method because it's an object. And car would also have properties. A property could be miles. So how many miles is done? Another property could be how much fuel it has left. Another property could be, I don't know, the color. Another could be the make. So it can go on and on forever. So this is kind of the basic concept of an object. So when you have like a real life character or pretty much, it can be useful, a lot of things, which is why it's very useful. So in this scenario, the car is the object, and we have a few methods, one for reverse, one for forward, and we have another, and we have the same object right here, car, and we have some properties to it, like miles, fuel, color, and make. So this is just to give you a basic idea. This is not actual code, it's sort of like pseudo code for you guys. So now let's go ahead and actually create something. So let's assume that we have a system in which we need to register different type of students, and then once we are done registering it, we need to have a function where when we type in the function, I mean, when we run the function or the method, it should return the details of the student, as well as the year the student has graduated from in college. So let's do that really quickly using object-oriented programming. So thinking about it, we need to have first a parent class. So we're going to create a class, and now I'm going to call this parent class person, because so we need to have two classes, keep this in mind. So I'm going to comment this up for now, the second one, the first class is going to be person, the second class is going to be student. Now, if you try and think in your head, every student is going to be a person, right? Which is why it's going to have to inherit this, the properties and the methods from the person, because hey, a student is a person at the end of the day, so it's logical. So I'm going to create a class called person, which is later going to be changed into an object. Remember what I said, we need to start with classes first, and then they changed into an object. So class, person, open, close brackets, and then begin. Now in every class, you need something called a constructor or an initializer. So in JavaScript, it's called a constructor, but I think in Python, it's called an initializer. So it's almost like a function. So def init, def, okay, actually you need two underscores then init, and two underscores again, and then you need to give it some properties. So keep in mind that this right here is like an inbuilt function. As soon as the class starts running as an object, the first thing it sees is going to be this right here. Now in here, you'd usually mention the arguments that you want to gather. So in this object right here that's going to be created later, I want the person's name, so that's the first argument name. I want the person's H and I want the person's gender. Right, so that's the three things that I'm going to store about the person, right? I'm going to store these three things. Now these are just arguments. Now if we want to assign these arguments as actual properties to this object, so we need to actually attach those arguments when we gather them as actual properties to this object, what we need to do is also I forgot to type in before, we need to, the first argument needs to be self. So self refers to this object or whatever object it is going to be created as. So we do self.name equals name. So when we do self.name, self refers to this object or this class right here, which is going to be turned into an object later. So whenever it's an object, it's going to be person.name equals name. But since it's still at its class stage, we just use self and then whenever it's compiled into an object, it's automatically going to be person.name, right? That's what you need to keep in mind. So we're just attaching all the arguments as actual object properties. So self.h equals h and finally self.gender equals gender, right? So that's our initializing or initialize or construction function done, completed. Now in here, I'm going to do print or data is saved. Now, since we've done our initializer function, we've also set as object, which is person. We've set some properties to it, which is the name, age and gender. So we've learned how to add properties to our object. Now let's go ahead and see how to add methods or functions inside this object. Pretty similar to how you do it outside an object, you would do it inside an object the same way. So depth because we're defining a new function. The name of the function is up to you. Now I'm going to call this function. Let's call this show details. Cool. So I'm going to call this function show details. You need two brackets. Now I don't want to add any arguments here. If you wanted to guys, instead of adding all of this in here in the init, you could have done this over here in the show details. But the best option or the best practice is to pretty much gather all your arguments in the initializer function itself because that's the first thing that runs up. Now if you want us to fill it separately, then you could have done it over here. But since I don't have any arguments and all the arguments or the information that I need is already stored as properties here, I'm going to type in self. So when I type in self in here as an argument, all of the properties that are available to this class called person on all the methods as well are available to this show details function. Without self, it's not going to be available. So we need them. So I'm going to print details. Or let's say post new details. I'm going to do a lot of prints. And let's do self.name. Now whenever you're referring to these variables, you need to make sure that you type in self because remember that this name right here, you've assigned it as a property to self. So you've assigned it to the object. So every time you're referring to a property in the object or a method in the object, you need to refer it as a self and then type in the dot name. So name, age, self dot age. And then lastly, we're going to do gender, self dot gender. Now we're going to run this quickly to show you guys how this is going to work. So at this stage right now, hopefully we have no errors. At this stage right now, we just have nothing showing up. And as I said before, guys, we're not running anything or we've done right now. So if I have my code side by side, all we have right now is pretty much we've created a class. This is not yet an object. So to turn this into an object, you'd have to, you could do this in your main code right here. But I want to show you in my compiler because it's a bit easier to explain. So I'm going to give my person a name. So I'm going to be Johan equals, since this is just a variable that I'm assigning to the object that's going to be created soon. Also guys, I forgot to say, when you're creating a class, the convention according to Python is to use a capital letter at the start. So try and do that as much as you can because it's best practice. So since it's a class, I'm going to use capital P person. Now when I open bracket, notice how it says what arguments it needs already. That's because you've mentioned it in the initializer. So now that's a good thing as we know what we need to define here. So I'm going to do name Johan, comma age is going to be, let's do 21 for now. And then gender is male, obviously. And then let me close the bracket. So that's all our needs satisfied. Press enter. And then if you guys remember, as I said, the first thing it's going to go into is the init function on the initializer. It's gone ahead and taken the name that I've given it Johan. It's taken the age that I've given it 21 and male gender that I've given it and then assigned it inside the object as a property. So now if I go ahead and do Johan dot name, guess what? It tells me my name because as I said to you guys before, once it's an object, it's going to assign the self. The self would change to whatever variable you assigned this to. So now all of these properties are going to be assigned to Johan. So I did Johan dot age just 21 and Johan dot. See, even if I do a dot and then wait for a couple of seconds, I have options right here because this is how classes work. After the rendered is an object, the compiler is able to pretty much, or the code editor is able to see pretty much all of the methods and the properties, which is very useful. And now since I've showed you how properties work, Johan dot age, Johan dot gender and all of that. Let's go ahead and do Johan dot show details. Now in here, we don't need any arguments because we said just self so that we have all the details available. Now these are properties as you remember before that we've just played with. Now we're playing with the procedure or the method that we would call since it's inside an object. Enter and voila, it says personal details, name, age and gender. So as you guys can see right here, even though this function is different, since I've passed through self for it, it's able to get access to pretty much all the properties that have been mentioned before. And the function is called as soon as I do the object name that I've created here as a variable dot the function name or the method name. So that's all when it comes to the basic use of an object. You guys can play around with it, create your own versions and stuff. Now for a bit of the complex part, we also spoke about inheritance which you guys could if you guys can remember. So we need another class which is going to be a student class because as I said we need student details, we need to have a function that shows us details. And then that function also shows when the student graduates. So when we have a person class, it's a parent class because these details are general. Now a person can obviously be a student, right? But then a person doesn't necessarily need to have a year of graduation because he's not specifically a student. So we're going to have to create a new class for this because a student needs a year of graduation so it's a different concept compared to a person. So I'm going to be inheriting some of this information from person and then we'll be using that stuff in student without actually repeating code. So I'm going to show you how that's useful. So type in class student with a capital S because it's a class and then in the brackets we're actually going to mention person because we want to tell our student class that we want to inherit some of the details from the parent class or person, right? Which is up here. Now same way we're going to have to use a colon exact the same way we create an initializer self again and in here we need to mention the previous field. So name, age, gender and I also need year of graduation now. So we need to mention these since we're going to be gathering those details as well. Now next what we have to do is going to be a bit different. So we're going to do self. Okay before we do that we have something in Python called super function. So when I run this I can do super in it. And now in here if I type in name, age, gender what goes ahead what it goes ahead and does is it's going to look for the parent class which is in here person and then whatever fields I mentioned over here which is name, age and gender it's going to look for them. And then I don't have to type in these three lines over here. It's the equivalent of doing that so it's going to automatically do that for me. So this one line right here is going to be doing the job of these three which is assigning these arguments as properties to my object. Now since I've got these three already assigned using this line I still need to assign my object this year of grad as a property. So I'm going to do that self dot year of grad equals year of grad. Right so you don't have to assign it each time or you don't have to use the same name this could have been year of graduation but as long as it matches the argument name you'll be fine. Now for the important bits we're going to have in here a function. So we're going to have another function which is going to be def show grad date. So it's going to show as a graduation date and it's also probably going to show some details about the students. So what I'm going to do is this is just like previously we need another method inside a different object which is going to be created later but this time it's a student object. Now in here I need to pass self because I don't need any arguments or the arguments are in my initializer. And I'm going to go ahead and use my show details function for my person class. Remember when I said as we inherit the details for my parent class all the methods and all the properties are available to us. We don't need to repeat ourselves. So that's the pretty much I find this feature a lot more useful than the other paradigms that I've worked with. So I'm going to go with show okay actually we need to type itself because remember it's we're still in a class and we don't really have a name for this object so we're still in a class we do self.show details and then just run it because we don't need any arguments in here. Now we go on the next line. So show details is only going to show as name, age and gender. We also need to do another print line which is going to be the year of graduation. So let's do year of graduation. Comma self, self dots year of graduation. Right cool. So hopefully there's no errors. Let's run this quickly and no errors I am assuming so we do your hand equals. So you guys might um so this time around if you notice we've got the same fields that we need in person. We've got them in student so we don't need to do the person because obviously we're not looking for just the details but we're looking for the year of graduation as well. If you're looking for just the details go ahead and do it using the person class but I'm looking for the year of graduation too so I'm going to go do it in the student class because that's where I've done my inheritance as well. Inheritance I mean sorry. So I'm going to do your hand equals student so that's just giving a variable and assigning it to my class then as soon as I put the brackets in that turns into an object and I need to pass my information as well. So your hand oops comma 21 comma year of graduation let's just say 2022. Cool let's press enter and it says all data is saved. Now bear in mind I'm using my student class and not my person class and if you see in here in the student in the person in the student class I mean there's literally no way that says all data is saved. So once again what I said about inheritance it has literally inherited this information in here so it's pretty much grabbed the whole initializer here and then it's using that because we've used it over here we've said we want to use the super to grab the name age and gender and pretty much anything that's after it will be grabbed as well because we're pretty much inheriting everything from the person class into our student class and addition to whatever more we have in the student class too. So now for the main bit we do your hand dot show okay perfect show grad date and in here remember we don't need any parameters so press enter and here we have it personal details your hand age 21 in the mail year of graduation 2022 and the only thing that we have done in our student class is mentioning the year of graduation the rest of the details have pretty much been inherited from our person class and that's how object-oriented programming can save you oh tons and tons and tons of lines of code when it comes to like massive deployment of code and stuff obviously it doesn't make sense on a smaller scale like this but in the next tutorial I'm going to show you guys how to create a little banking system using object-oriented programming that will put a lot more structure into why this is so useful and so organized and I don't know it's just a lot more easy on the eye and easier to manage in my opinion because if I wanted to change anything to do with the person I could just go in the person class if I wanted to change anything to do with the student I could go in the student class and hey if we had another class for teachers maybe we could just inherit the person class again because obviously teachers are a person and then add whatever else we want it to it so that's it for today's tutorial guys hope you find it useful I had a lot of fun teaching you guys this I've learned object-oriented programming very recently and I was quite frustrated because I found it a bit hard to understand in the start so I recommend you guys to look up a few exercises so probably try and re-watch the tutorial if you don't understand and then do this by yourself without looking at the code so that you guys kind of get used to the concept trust me in about two weeks you'll get used to the concept and you will be loving it so anyway guys if you guys have any recommendations for future videos as I said before drop them in the committee tab under my post and I'll see you in the next one guys please also make sure to like, comment, subscribe and share because sharing is really important right now and peace out