OOP with TypeScript: Inheritance

TypeScript
Transcript

English (Auto-generated)

about the inheritance. So what is the inheritance actually and why and how it helps. So for example, for example, um we have, you know, a class which, you know, if I just, I'm just, you know, I just deleted everything. I'm gonna do that again. I mean, I'm gonna believe that just showing you this is one class. Okay, this is a class which is, you can see that a person class and that is understandable. This is one class. But um mhm. Um this is generalizing every single person, but maybe, you know, every person has a full name. Every person has aged, every person has the hair color. Every person has the height, Every person has the smoke's grenade grenade. I mean general mm hmm there is no time for bombing party. Anyway, everybody has, has to be pronounced by some something like, you know, his or her or whatever. So these are the basic things and you know, this is the general human, a general person. But what if we talk about some army personal, because we already talked about the grenades legislate some army personnel. So maybe the army personal, when we are, you know, talking about the army person. So the army person must be having a gun must be having, you know, um a rank in army must be having, you know, uh kind of special training or something like that. So that that is a criteria for an army personnel. So here, um what we're gonna do, you know, no problem if the army personally has something like, you know, the which gun he holds or um you know, um, what rank of the person is or things like that. But ultimately every army personnel also has the name, age, hair, color, height, smoke or not? General things are like that. So these are the basics of every human. So we can consider it a super class. This is a class. And then we are going to extend this class into multiple classes. So every, what we're gonna do, we're gonna create another class for the army personnel. And in that we're gonna put, you know, the specific properties like, which only, you know, should reflect as an army man. So, but we're not gonna call or create these properties again, we're simply going to extend this class to that new one. So ultimately, whatever it is inside this class, the person's class will be inside that army personnel class ultimately, and it will work amazingly. So I'm just going to delete everything Now. I'm going to create a simple class again and then I'm gonna create the army army personnel class. I mean, I'm not gonna use the army personnel class, I can use anything, but just, you know, person, a general person, it can be anyone, it can be a teacher, It can be, you know, a student or something. So the person and every person has a full name, you know, and which should be string. Every person should have the um, age, the age of the person and it should be the number, every person should have the you know um Pure color. Okay and then every person has a general, okay this is a general class. So if you're creating um you know some object for example you know P. One again count P one equals two new person. And now absolutely you know uh P one dot full name. It can be anything like you know um johnny Okay previously I was using john and now I'm using johnny. Anyway P one again P one dot age and here the age can be 54 and oh sorry this has to be this way and then you know the P. One again P one dot hair color equal to black. Okay the P1. Equals two. it's a journey. So it's a male. Okay. That's awesome. Now um now you know we already have what we were looking for but you know it is some students, you know a student and teacher should be categorized differently. So if the person is a student in case it's a student so we need to have some more things you know like in which class he or she is in and which school and what he or she is learning and things like that. So if you start writing here for example the um you know a school name that would be pretty awkward because you know not everybody is in the school name. For example a teacher is a teacher isn't in school, a school by the way, but it's uh, not for studying, but for, you know, teaching. So let's say a barber barber cannot be in this school. So I'm, what I'm gonna do, instead of, you know, I'm going to create a new class student. Okay. And in this class, you know, only students would be able to use this class. So we can do two things. We're simply going to copy this one and then go down and uh um instead of copying, let's just, you know, I was just trying to copy because I, I was trying to explain it a little more in, in a little more depth. I'm still doing that. But without repeating the code anyway, a student can have a school name which has to be trained and then the student, um, the class level, it can be an old level A level or anything. And now I'm going down after the class level. I'm gonna write something like, you know, um, grade or let's say last grade, it can be anything. Last grade, um, string again. Okay, I think these are enough. So now you're gonna create, you know, you're going to create an object foreign students. So what is you are going to do right now? Um, Every student has a full name. Every student has the age and the hair color and the general, and every student has a school name absolutely in a class level and absolutely has some great previously, you know, so what we do, we do something like which is called inheritance and we do it by extending. So I'm going to create a new class. Actually, what I'm not, I'm not going to create a new class. I already have created two classes. I'm just gonna go there and I'm gonna add any space and I'm gonna write extend space and the person. Okay. This sounds little self explanatory. But if you don't get it, let's talk about it. Every property, which is in the person is now the part of the student. Okay. Every property, for example, a student is now have the full name now have the age and something like that. So let's just go down and I'm creating another const. S one Equals two new space students. Now I'm going to add a student with this new object. S. One. Okay. So I'm going down and I'm writing S one dot, you know, I got the school name, I got the class label. I got the last grade. I also got the age, full name, general hair color. So you know, um, the age is the person, it's coming from person, but it is available here. You can see that if I talk about the school name, it's coming from the student class, but it is there and can be associated, can be assigned the value for the school and can be assigned anyway. I'm going with the full name and the full name I'm using as you know, a student. Full name is. Hey mom. Okay. Emma and S1 Age. I'm writing it's 19. Okay. It's one dot general hair color is not really necessary by the way. But you know, so it's a female. Okay. It's one. Sorry, hair color equal to brown. Okay. Okay. Now S one dot um we already edited age. Full name, school name equal to you know, be public school, whatever. There's one that class level high. Okay. It is it can be A level. Okay. It's one um last grade. Okay. She got like a 60 of the grade is, you know, it should not be the string. It should be a number anyway. Or I mean we can go with the grade to, let's say she got the B grade or B plus. Okay be positive, be positive please. Okay. We got the we got everything here. Now if I simply, you know, control lock this and I write S one and I say that I go there and I write the TSC and mind break and now I'm gonna get I just can check that. I mean it's not necessary but you can see that you know, um things are here and now I'm just gonna write the node spaces, script dot Js and lined brick. Okay. You can see that um you can see that the full name is Emma, everything, you know the female, the brown and the public school. So this is how you extend the class. And I think the concept is pretty obvious and pretty simple. You're just, you know, extending this class with the new class with, with an an old class. So for example, we have another class. Like teacher just telling you teacher so we can extend this teachers class with a person's class or we can extend this class with the student class if you want to and now, okay, let's say for now the teacher class is actually empty. We didn't add anything inside this, but it already has all these properties. The full name, age, hair color and the general. So the teacher, the teacher can have its own classes or the teacher can only inherit, inherit whatever the person class has. So I think that's quite simple and explanatory. So the teacher is still active. If I create a new object, let's say I'm going to create a new object right after that because you know, const T one L. T. one equals 2 new space teacher and save it. And uh here T1. know, we already got the eight full name, general and the hair colour. So if there is something, let's say I'm gonna type here in the full name. Um Larry the teacher whatever. Okay, so everything we have in inside the person is now the part of the teacher. So I think this is pretty much simple. If you if you want to have your own things, you can add that. We didn't have the functions or things like that. But, you know, this already explains a lot about the inheritance. So in the next cast, we're going to talk something more to like stay tuned.
89 Views 0 Likes 0 Comments

Let's finish off our Series on OOP with Typescript by discussing inheritance.

Comment
Leave a comment (supports markdown format)