OOP with TypeScript: Encapsulation Part 1

TypeScript
Transcript

English (Auto-generated)

important when when when we were doing anything in the object of indian programming, no matter if it's typescript or any other programming language, it supports the object oriented programming. The O. P. We're talking about the encapsulation. So first of all, what we need to do is um we need to, you know um create an object. So I'm going to create a class again. I had a class already created in the previous cast but I intentionally deleted that because I I wanted you to you know, I want you to practice again and you know uh practice makes better. So the glass and then the name of the class can be anything. I'm writing pure person or you can say human or whatever you want. So that's uh an MG class for now. And in inside the empty class, what you're gonna do is we're gonna create a few things. For example, we are going to call it the name of the person named. Sorry, we just can't call the name, you know? So um key name, let's just say the name is is kind of awkward but uh the name or what should we write, let's say full name. Okay, that's a that's a perfect explanation. Okay, the string and then the second thing I'm going to create here is the age of the person. It has to be the number. And after that um you can call anything, we're gonna call the hair color and the hair color has to be the string and then we're gonna go down and the next thing we're just, you know, asking here is uh um height, height has to be the string again. Okay and smokes, It has to be the bully and maybe it does in its smoke or maybe it does. So the bullying. Okay, a few things we've, we've just done here now the thing is um, you know, generally if you are just, you know, um creating something like, you know, uh the const the name of the person you're writing, I mean T one, let's say P one, it's a rule number, whatever you wanna call it. So the P one equals two. New and space person. Now you're you assigned this class, this whole class to this person. Uh the P one. Now the P one has all the properties we have in the, the person class. You know, this is how the blueprint works, but we had a discussion that, you know, you might know about the interfaces in the typescript. So what is the difference between the classes and interfaces? So now in this cast we're gonna discuss the actual difference between both of them and why the classes are more, you know, a lot more at a whole, a whole new level, a whole different level than the interface. And this is actually the object oriented programming. So, um what I'm gonna do is, I'm gonna, I'm gonna do something like p one dot um p one and Full name, like I did equals two and the full name can be anything, it can be, I'm writing John it's a full name. So I'm writing john mark the p one again dot age. The age equals two. Um Writing let's say 40. Okay we hear color. The p one dot here. Color equal to I'm writing black. Okay that's a black one. P one. That height. Okay now there are things coming here and I'm gonna tell you why how in calculation helps? Okay 1/68 cm. That's fine. And now the one that smokes equals two. I'm sorry. It has to be true. Let's say let's say true for now. Okay that's awesome. Now for example you want to do something like you know if the P one actually smokes. Uh I mean the people he smokes is equal to true then we have to do something, we have to tell something or if the height of the P one is more than 1 60 then we have to, you know we have to do something with that. So um This is one concept and this requires the encapsulation. You're again going to go to the class and you're just gonna make it private private. Now this is the private one. So you just cannot access it here. You can see that the property hide is private and only accessible within class person. So now you just cannot access the hide outside of the class even though you've assigned The class to this person I mean this person to this P one object. So you actually inherited and not inherited actually, but you know you just meet you just meet this one uh an object using this class. The person class. But the problem is the height is private so you just cannot access this outside. What's the person I mean what's the purpose of the height when you just cannot access it with the object? I mean wherever you are assigning. No problem. No matter you're assigning multiple, you're creating multiple objects. You just cannot use this height. The solution is you know, you know it tells that the property is private, the property high, it is private and only accessible within the class person. So you need to go back there and you need to you know like that. You need to create a kind of function right here inside the inside this class. So that's what I'm gonna do right now. The um get hide height. Okay. And inside this function. Wait a minute. Okay I was just having a kind of a missed call from, I don't know somewhere someone anyway. Okay get height and inside that um what we are going to have is the here color here color and it is the string and that's totally understandable. Okay now this is I mean we can write something any anything here like para one. I'm intentionally making it para one because I just don't want to confuse you now. Inside this function, I'm going to call this here color because we know that, you know, we can call the private properties inside the person's class. So even though we're inside a method, this is a method, we're inside this method which is inside the class person, so we can call this high property here, but how to call it, we just cannot call it here color. You know, I just wrote here color, I got this dark hair color. So if I again try this here color and I press the line break or enter key, you can see that we got it. And what is the meaning of that? What's the meaning of this? I mean, this literally means this, I mean, you know the way we call this in english? So this, you know, because we are inside this person class. So when we're talking about here here color, we are definitely calling some property which is inside here. So we're, you know, this is actually pointing this hair color, I think that that kind of makes sense now. Okay, and uh we just got it this dot here color. Now, one more thing, we just, we just can't be here and we can make it here, color. Now, there is a tiny problem of one more thing that here color equals two. I'm assigning this dot here color to this hair color. What's the meaning of meaning of that? Now, when we're talking about this dot here color, we're talking about the hair color, I mean this hair color, which is actually the property of the person. But when we're talking about this one, the hair color. So ultimately, this is just, you know, a parameter which can be anything. You do not need to write the hair color, you can write it hair color or the body color or I mean, you know, you can write anything here as, as the name of the hair color. So just hair color. And I just took the parameter and assign some value, which is even chilly this one. So what is this thing? This is actually called the getter function in the uh, the class in the object, in the in the, in the simple javascript. Anyway, so this is a getter function by using the ghetto function, we just got the here color. You can, you can see that this property, we just got this property. And now we are just going to call the setter function and I am just assuming that this can be a little tougher for us. So, um, let's just uh, let's just move to the next video next cast. But there is a little thing I did wrong. The private is height not the hair color. Sorry. So, it has to be hyped, it has to be high it and it has to be high. Ultimately. Now this private property height is accessible here. One more thing, even though I was using the hair color, that is also acceptable because no matter if it is private or its public, we can call the values of the of any property inside the class in the ghetto function so that we can have multiple getter functions. Getter functions function is just the name of it for now. So I think this makes sense. And in the next cast we're gonna continue the same lecture with the, you know, the better function until then stay tuned.
115 Views 0 Likes 0 Comments

Let's talk about encapsulation.

Comment
Leave a comment (supports markdown format)