1.16 OOPs concept in Python | Python Full course 2024 #learnpythonin2024 #mlwithakansha

In this tutorial, Akansha Yadav introduces Object‑Oriented Programming (OOP) in Python, covering the five core concepts: classes and objects, constructors (init) with the self keyword, inheritance using super(), encapsulation through protected attributes and getter/setter methods, and polymorphism via a common interface. She demonstrates each concept with concrete code examples—a Car class, an ElectricCar subclass, and a Student class—showing how to define blueprints, instantiate objects, reuse code, protect data, and treat different class instances uniformly. The video emphasizes the practical benefits of OOP for code organization, reusability, and interview preparation.
Chapters
Ladies and gentlemen, in today's video we are going to see what is OOPS in Python. So OOPS is basically, uh, a way that we use in Python for writing codes. So that revolves around two things: class and object. These two things are the key concept in OOPS. What is— let's first discuss the full form of OOPS. OOPS is Object Oriented Programming. Object-oriented programming that uses object to organize and structure the code. It can be any object to organize and structure the code. So basically, Python is a language which is multi-paradigm language which supports OOPS also. So OOPS is— everything is in OOPS, everything is object. You can use the features of OOPS such as classes and object to design and implement your programs. You might have done basic coding, but OOPS is something which can take you to another level, your coding skills to another level. So the very first concept in OOPS, which is object-oriented programming, is the classes and then the object. So first, let's see what is class. So basically, class is the blueprint or the template, we can say. In simple words, we can say that this is a template for creating objects. So classes are basically templates for creating blueprints. You can consider classes equal to blueprints. Another thing is the object. Object is the instance of the class. It means the part of the class. If blueprints is a word, then Print is the object. Like this, it is a concrete realization of the class with its own unique data and behavior. Let's take an example. Suppose there is a car. Let's take an example, say that there is a car, okay? So the car has two things. One is the making of that car, another is the model of that car. There can be any model also, there can be any make, company, we can say. Another thing is, let's— this is what we call, suppose we are defining a class, we are saying class Car which is having two things. One is the make, another one is the model. We have defined these three things. This is an information about the class. This is what the class is. Now this is the blueprint. Obviously this car is the blueprint of both the things, but now I'm creating it. Let's suppose an object. So object is, it will inherit something from the class and will have its own characteristics also, its own behavior or data also. So in case of object, car can be— let's suppose I'm saying car, which is I'm taking from the class car, and then I'm saying suppose Maruti or Toyota. Okay, I'm writing Toyota and Camry. And then I'm saying another one car, which is the second object. First object is this, second object is this. I'm writing Honda, and this I'm saying Accord. So there is a model, there is a Make also. So these two are the objects where they have their own data and behave— the data which is the name of the make and the model. But in case of car, you are going to create just a template, basic template you will be creating, and using that template the objects will work. These two are the basic things. So let's write it down in the form of code. So I'm going to create one class. Let's get back onto the screen. Okay, so let's discuss this code. What the code says— first is the class. Class is what? Class is the keyword in Python. Then again, I, as I said, this is gonna be a variable, class variable. Class is the key term, it means a keyword. Then you will write whatever you wanted to write, whatever class you are creating, whichever thing you are talking about. Right now I'm talking about car, so I have written car. Then you have to write double underscore init double underscore, and after bracket you will say self.make_model. def self.make_model. These three are very important thing. Then if you can see that I have written init__init__ then I have written these two things. So what I wanted to say over here is I am writing it within the word called def. Def is for function. So I'm creating under the class, I have created two function. One is the this one, another one is this one. First let's discuss the first one, why I have written init and what is the meaning of this init. Why do we have two underscore in this? So one by one we'll discuss this. The first thing, let's— as I told you, first is the class Card, and now I have defined this. So __init__ is the, uh, is the method, we can say, is a special method. We also call it a constructor that initializes the object's attribute, whatever object's attribute, whatever template you are trying to create, where from where you are beginning. This is how you denote it. So you will write underscore underscore, then init, then underscore underscore. This is a special method, we call it constructor also. So there are two things under the object's attribute. One is the make, another one is the model. And another— after writing this, what we have done is we have used one more function which is called display function. This is just a normal Python function where we have written display information. Self. self means this is the keyword. self keyword is very important. self keyword denotes that, okay, this is something, you know, within the class with the security, like that. And then we are writing def display_info, information I want to print, and I'm using the normal print statement to print it. So this is just a normal function in Python which you might have covered in the last class, last video, and This is from the OOPs concept. So let's get back onto the objects. Now what is object over here? There are two objects. Car1 and Car2 are two objects. One is with the Toyota and Camry model, another one is the Honda and the Accord model. So two instance of the class Car are created over here, and during object creation we are using init method which is automatically called, like init method. With the init method, if you are not going to write this init one, this one, if you are not going to write this, you won't be able to call anything in the object. Once you are writing it, then only you are calling it. So that is why we use constructor for this purpose. Then after that, what I have written is I am printing both the things. So here I have the function called info, display info, and I am writing it car1.displayinfo, car2.display info. Let me select the kernel and once it will work. So here we are getting the name of the car, name of the model, and name of the make. So basically this is the car Toyota Camry model, car Honda Accord model. Like this, oops, classes and object works. Another concept in OOPS is the— we do have 5 in total, but 4 are more important. Like, first we have discussed object and classes, and this was the first. If you are going to consider it like classes one and then object one, then you will have 5 in counting. Second is the inheritance. Inheritance is something that allows a class to inherit the properties or the methods of another class. Now this time In the previous example, we were having 2 objects and 1 class. Using 1 class, we have created 2 objects. But now I am talking about 2 different class, and using those 2 different class, I am going to take some properties or methods of another class, which is— which may be a superclass or a base class, or whatever you are going to define, parent class or child class also you can see. So it's just like the hierarchy, like in human also we do have hierarchy. We take some, like we inherit some You know, maybe the way we are talking today or way we are looking, everything, whatever, not everything, like few features of ours are from our parents. So that is what inheritance is. So hierarchy is everywhere. So in OOPS also we do follow that. In inheritance, we are using two classes. One is the maybe parent class, another one may be the child class. Basically, how this is useful is it promotes the code reusability and enables the creation of hierarchy of the classes. It means you don't have to define the classes again and again, just use a different variable, define another class, use some features from the above class, and then re— then use it and then write it. So let's write one. So I— let me just quickly write one code. So we already have a class called Car. I will use the same Car class. So yeah, I have written the code which is using the Car class. So I have class ElectricCar now. With the bracket, within the bracket I have written Car, which is the above Car class which we have already defined. Again, we have to— you have to use the constructor every time whenever you are writing the class. Now this time what do I have is I am taking make and model from the above class. Another thing that I have is the battery capacity. So I'm writing super.__init__ . Super.function is also a function. So over here, ElectricCar is the name of the class. Then we have init, which is the constructor of the ElectricCar class, which is defined to initialize both the attributes which we have, make and model. Then we have new attribute, which is battery. Now then, we have— what we are doing is we are writing over here is the object. Object we are trying to say, we are saying that electric car Tesla model this and 100. So we have already defined one object over here and we are trying to, using overridden method, we are trying to derive it from the class. So let's run this code and see what is gonna be the output. So first is the Tesla Model S. Model S, Tesla Model. Tesla Model is from the class 1. Another one is only in this class, within the ElectricCar class we have defined one feature which is the battery capacity, nothing more than that. And this is from the first class. We are— we have already inherited. This is what inheritance is. And this is the original one from the second class. So that's how the inheritance process work. It means reusing the first one which you have already defined above. You will use it and you will create more features. Another concept in Python is encapsulation. So encapsulation is also another important topic because in encapsulation it refers to bundling the data and the methods that operate on the data within the single unit, that is a class. So encapsulation says that it internal state of the object and can use gather— like simple, in simple words, if I would say, then it is a best way to protect your data. Whichever data you wanted to protect, you will write underscore in front of that. That is what encapsulation says. With the help of underscore— underscore is very crucial in programming languages. So once you are writing underscore, you have told to your programming language that this is something that I wanted to encapsulate, keep it within the coverage. Few people should access this, not everyone should get the access of that particular thing. This is the protected data, or this is the particular protected attribute. So that's how it works. Only if you have the getter method or setter method, then only it will come to you. Otherwise, no, it won't. I will show you one code on, on the same encapsulation. Okay, so finally I have written the code which says that, okay, we have a class called Student. Within the Student class, we have def init. We have defined two things, name and age, but I have written underscore this time. Earlier I was not using any underscore before any, uh, you know, any word. So I'm not using any underscore over here, but this is that concept of encapsulation. So that is why I'm using underscore over here, _name or _age. _name and _age, both are protected attributes. If I'm talking about the getter method, it means you wanted to get that method, get whatever is being stored in that particular variable. This is what getter method do. We simply have to write def getName bracket self. Get, I'm writing get, and then I'm writing name. getName, this is my function name, and I'm writing this, and then I'm returning self.name. So directly I'm not going to access that. I won't be able to access the name and the age no matter what user have typed. Another is I am trying this time, like this was for the name and this is for the age. So in both the cases, I'm just creating two functions saying getName, getAge. Both are the name of the function, and then within the bracket I am writing self. With the keyword self, they will automatically take it. And what exactly I want from entire, uh, information is I want _name, which is protected one. That is why I'm using underscore. Then in case of setter method, what I have to do is setter method is in the case setter method is in the case where you wanted to apply some methods onto it, or you wanted to perform some more operations on that. So if I'm writing set_age , it means I'm using the age parameter out of it, age attribute which is protected above, and I'm checking whether that age is greater than that or not. If it is greater than that, then only I want that particular age to be printed, else I don't want age in the output. So that's how our encapsulation works in Oops. While creating the object, I have created an object. I have written the name and the age of that person. Now let's see how getter and setter method works. In first case, what I have written is student1.getName. getName is the function which we have defined here, and this will help us to get the name of that person. And second time I'm trying to get the age, but this time I'm not trying— I'm, I'm trying to set the age. If it is true, then only it will print, else it won't. So let's run this. So we are getting Elise, 20, and 21. We are not getting anything because over here we have not printed it. Over here we have said that if it is greater than 0, then set that age to age. Whatever you are entering, you will just get it saved in that particular age variable. So earlier when I defined the object, I was having 20 while definition of object, but over here I have changed it to 21. I am setting the data. I am trying to— this time in getter method I was trying to get it. This time I'm trying to add, I'm trying to change the value. So that's how encapsulation works. The last concept in the OOPs programming is the polymorphism. Let's understand what polymorphism says. Polymorphism is something that allows the object of the different classes to be treated as the object of the common base class. So let's suppose there is one, there is one, uh, I would say that chain. So there are chain like this, and this is the base class. Above, below the base class, there are 1, 2, 3, 4, 5, like 5 different classes. So I want to have a one base class. Once we have one base class, this is what the concept of polymorphism says. So this is what polymorphism says. So it is, and it enables a single interface. It means even though you have multiple things to do, even though you have multiple classes, different objects, but you are going through this, we are promoting the code flexibility. It means if I am the programmer, I have created one class on this, I can use my— another friend can use it too. Like that it works within the same file I'm talking about. It's not you are working the different file and your friend is working on the different file and there would be some magic and this will take place. No, this is not like that. In the same file when we collaborate with other friends or other colleagues, polymorphism can help you. Even though if you are programming and you are creating entire model on your own, then you have the access that you can create a base mod— base class, and using that base class and adding all the features of from all the like different classes into one. I will just show you an example of the same. Which is related to our class Car class. Let me write. So here I have created a function which says displayVehicleInfo, and I am writing vehicle. And what is vehicle? I'm saying vehicle.displayInfo. This was my function name. I am calling my function. Whatever was stored in the car1 variable, which we have already defined above while we were learning classes and object, over here. And in the second example, we were using electric cars. So let's run this. In first case, we are getting Toyota Camry, which was due to this. Then car1 says that Tesla Model S, this one, electric car1, which were having a battery capacity also. So this is how polymorphism works. All the 5 concepts of OOP programming language is very, very important. Even though if you are not going into the developer field, you should learn this. Interviewer might ask it. It may not be that useful, but yeah, if you are writing a lengthy code, this is very useful thing to use. So we'll meet in the next video. Until then, you can comment your views, you can suggest me some video titles, I would be happy to use them.










