OOP with TypeScript: Abstract Classes

TypeScript
Transcript

English (Auto-generated)

this video. We're going to talk about the abstract classes. It is also a thing, an interesting thing and how we can use them and what are the benefits of them, actually. So, when you're working with the abstract classes, actually, you create some classes, which just cannot be instance. She hated. I mean, you just cannot create in object using those classes. What does that mean? Let's just, you know, assume, you know, you know, you have a class, which is which name is person. Okay, So, the person, uh, let's just make it this way. Okay, And you have a class which name is a student? Okay, So, or let's just call it human. Okay, So, every student is a human and, you know, you create another class. And in this class, this class is um, let's say, um, businessman biz this man. Okay. So, every human is a businessman. Every human is a student. I mean, sorry, every student is a is a human. Ultimately, an animal cannot be a student. At least a good student. Every um, every businessman is a human has to be a human to do business because animals cannot calculate. So, I mean, you do not need to any instance, she ate a human itself. But you absolutely want to talk about some businessmen. You know, you're not talking about the basic human here. You're talking about the businessman. The student. But because every businessman and every student share one thing that both are human and both things and acts like a human, but absolutely both have different skills and different professionals. I mean professions. So, how you can do that, you know? So then you, you have to create this class as an abstract class, but this will, will not be able to be instant. She ated how you can do that. So, first of all, I'm just gonna go inside the human human class. I'm making the name and because we talked about the string Okay, I just cannot call the name because it's a reserved word. The full name. Okay, I'm making it public if you want to make something public, we do not need to because, you know, ultimately the public class, the public class ultimately, you know, is something set by default. But anyways, okay, now I'm going to the next one. I'm making another public class. Again, you do not need to write public if you want to make it public. But writing off makes things more, you know, constructive and more connected and more readable. Anyway, so the public, another public class is um, age and this should be the number. Another public class, which is um um, what should be the next class? The full name. The age, the dinner. Okay. And it has to be the string. Okay, that's awesome. Now, um, for now, if we want to, you know, we are extending this class, extend human. Sorry, human, Okay, whatever this class has now, the part of the student class and if I extend the business class to human now everything the human has is the part of the business class too. So absolutely both of these classes, you know, are reflecting all the properties and if any method we've created in the human class, understandable. Now I'm going there and I'm writing um The let I'm creating an object stu or ST one Equals two new student. Okay. So when I write students, you know, because we didn't create the constructor functions anywhere. So we have to go through this typical method. I mean both work. So we're going with the, with this one for now equal to I'm writing john here and that's totally understandable. If I, you know, I go to, I go here uh the SD one dot full name will show johN to me, but if I write let Human or H one H 1 equal to new human. Okay, that is also a thing. And now I'm going down and I'm writing um H one equal, sorry, H one dot full name equal to some or let's just call it lara, I was, I was about to write be some human but you know, I'm making it more easier for us to understand. Okay, anyway, I'm going to be consoled the log, console, the log and here I am writing it one dot full name and I'm also writing console dot log um as t one dot full name. Okay, now I need to go to the, to the terminal and I need to write TSC. So I'm gonna get, I'm gonna have everything this house couldn't word it into the script dot Js. You can see that we've got it here. That's awesome. Now, I'm going to get their bag and I'm running this dot Js file. So script dot Js and then the lara and the john Okay, understandable. This is what we already know. But why creating a human when we have, you know, maybe you have a barber. Maybe you have a writer. Maybe you have a, you know, illiterate human but illiterate human is also a human. So we shouldn't, you know, use this class because this is the base class. We should be using the student or the businessman. So this this doesn't make any sense. I hope I'm making my point clear now. So just call it abstract. And now, previously it was not making any sense. Now, it is not making any programming logic because cannot create an instant instance of an abstract class. Now, you can call it a businessman, businessman because everything in the business, everything is in a human is a part of the business man. So the full name is still work. Will it still work? But it has to be a businessman or a student. So, I think it makes sense if I just go right PSC and I I'm not going to face any change for now, but at least it's not wrong. Now, the the john is actually a student. The the laura, laura is actually a businessman. You can see that the first, we're calling the H. One. Oh, this shouldn't be the H one, but this should be the B. One. It suits more. Okay, The B one. B. One. Okay. Save it. Okay. Now, you can, you can understand this is a B. One. Okay. The Lara is a businessman. So this is the concept of the abstract class simply. You just cannot instance, she ate a bit of an abstract class, but you can extend it and then you can use its elements. There is one more thing about the abstract methods inside the abstract class and we're going to cover that in the next video until then stay tuned.
173 Views 0 Likes 0 Comments

Working with abstract classes in TypeScript.

Comment
Leave a comment (supports markdown format)