âśď¸ Watch on 3Speak --- Learn Python Basics Together đ | Day 7 Welcome back to our Python learning series! In today's lesson, we dive deeper into Object-Oriented Programming (OOP) by learning three...
Transcript
Hello everyone, welcome back to my YouTube channel. So so far, we have covered many topics and out of which we have covered classes, functions, and whatnot. So today we are going to cover two more topics which are related to classes, which is inheritance and encapsulation. Inheritance, meaning that, let's say if you define something generic and then if you inherit something, so one of the example could be for that is vehicle. in vehicle you can say a vehicle can be of type 2-wheeler or 4-wheeler or it can be of different type and a vehicle can have a maximum speed as well so that's like default property so and then there are different types of vehicles so vehicle itself would have very basic properties and then let's say a 4-wheel would have default properties of a vehicle would have and and on top of that it would have its own set of properties so with that example we are going to implement inheritance over here, and then we'll also learn the implementation of encapsulation. So let's start with one default vehicle integration over here. So over here, this is our defined class, which we've already covered. So another thing that I want to highlight is that you necessarily, you don't need to explicitly specify the value over here. Once you write self dot value, it would understand. So let's say if I remove this, this also would work fine. So you don't have to, so this is one of the advantage of Python programming language that you don't have to say much of the things explicitly, and it would understand implicitly. So over here, in this class, I'm going to define initializer over. And in that, I would add some more parameters, for example, maximum speed. and yeah for now let's just say maximum speed and maybe the name yeah how about name so self dot max or simply speed equals max speed I'm gonna set and self dot model equals name so with this we have defined a constructor or initializer over here which takes two parameter and it's going to set the value for that vehicle so let's say if I want to create an instance of vehicle class so I'm gonna say generic vehicle equals vehicle over here and in that packet I can specify my speed which is let's say 80 and over here I'm going to say Suzuki let's say CIS so with that I have created an instance of vehicle now time for implementing inheritance so now I'm going to create a car class but In this example, we are going to inherit vehicle. So this is how we implement inheritance. So I would say this is the syntax for implementing inheritance. More or everything else, more or less remains same. So now I will say in it, and immediately it did whole magic. So I just typed underscore init and then hit the enter key and it wrote everything for me. So enough. For this one, the car I would also need, let's say, the fuel type, is it diesel or is it petrol or electric vehicle? So let's say fuel type. And that's like thought parameter, and in this case I'm going to set fuel type and I will accept fuel type parameter. So with that, now the car or say object of a car will have three different attributes, two which are derived from the parent class, and one additional attribute or property, which is of its own. So this is how you can inherit. So let's say a student class, and then let's say 8th grade student, 9 grade student. So something like that, you can also create. That was just an example. So I'm going to create an instance of a R. So let's say we call it Suzuki. In that case, let's say instead of vehicle we are going to create car so in that let's say next speed is 60 and for the name i'm going to write alto and for the third parameter i'm going to supply let's say petrol okay so with that now we have created an object of car which takes three parameters so this is how you implement in harrietans of course we are not displaying any value over here so let's try implementing polymorphism as well I think that wasn't the agenda but let's let's cover that as well now that we are going through inheritance already so in this example we declared initializer already now we are going to declare a function let's say so info and in this function it's going to simply print the spade which is the vehicle is having and in the model so let's try formatted string and over and in that we will use self dot name and yes we have to take the selfs parameter and now is having max speed off and then let's print the speed as well so with that you can see we are not only displaying the name but also showing the speed so right after line 21 if I start typing a generic vehicle and hit the full stop over here you can see the function which is we have defined now let me try running this file so if i right click and then run this so with that okay we're getting an arrow over here i know what mistake i did so we have speed but not the name we have model variable over here so that's mistake i did now i think it should work so let's try running this and with that you can see that it says suzuki cias is having max speed of 80 now let's try implementing a polymorphism So we are going to define the same function, so info. In the moment I start typing, it shows the definition of the implementation, which is already exist in the parent class. So it's going to auto-complete for me. So this is how you can call the superclass method. And after that, you can also write your own logic. So in this one, I'm going to say, fuel type. 4, and then self dot name, oh, it's modern, let's not do the same mistake again, is, and then self dot fuel type. So with this, we're not only printing the speed, but also printing the fuel type. So now let's do that, Suzuki Alto, and then, so info, let's try running this. So when I do that, it prints basically two lines over here. In this example, for the vehicle, Suzuki. also which is of a car type and that's why it's printing two lines one for the superclass and the other one for the class itself right so with this we've implemented inheritance and polymorphism now there's one more thing that we want to cover which is encapsulation so all these variables are easily available so let's say max speed in this one it shows variable right over in the pop-up and even though it was initialized to 80 I can change it to let's say 70 and if I try running this now you can see that it was modified to 70 what if I don't want to do not want the speed variable to be accessible outside the class and it should be accessible inside it so for that let's encapsulate the functionality or the variables that we want to so in order to achieve that we just have to use underscore underscore that's like favorite syntax for Python so in speed let's say if we want to keep it internal so or private this is how we can use and and now let's say so info is printing the speed but now if I try to change and run you can see the speed wasn't modified at all so speed is not accessible and because of that, it didn't change the value of the speed, which remained same. But what if you actually want to update the speed? So for that, you can write another function, and in that, you can write update speed, in which you will take one parameter, extra parameter, let's a new speed, you will be supplying, and now I'm going to update the value. So for that, set dot underscore speed equals new speed. So with that, now you can see that the function, is updating internal property. So instead of setting value like this, you can use function update speed and supply the value. And then with that, basically, you can have your own business logic, which is encapsulated, and expose only minimal things which are needed. And I find out this, the value was indeed updated, but via getter and setter methods. So this is kind of a getter method. It is not returning, but it's building. So, and this is a setter method. So getter method to encapsulate the speed value over here. So with this, we covered three things, inheritance. We were able to inherit vehicle, glass, into car. Then we also discussed about polymorphism. So info was defined over here, and it was over written over here. And the third concept that we covered was encapsulation. We were able to keep the speed variable internal and only accessible by certain methods. So yeah, this is it for this video. Thank you so much for watching. See you in the next one. Bye-bye.