Hello again, everyone.
So we've been talking a lot about TypeScript, right, so it's why don't we get into something else
called inheritance?
So, as the name suggests, inheritance is the name of the transferred genetic characteristics.
So I'll try to put it into context here and explain it briefly with an example, let's say that there
are employees working in a particular workplace and the customers who shop here.
So what customers and employees have in common is, well, they are a person.
And a person has different features.
So here we are creating a class where we can define common characteristics of people, so we create
a new class for different features, but we extend this new class to the first class.
Thus, we can use the first class features.
So as we will see in practice, I'm first creating a person class in here, I'm.
Writing a simple save method.
Then I'll create a customer class and extend the person class.
You follow.
So that way, the customer class now has the features of this original class.
So here again, I'll add a simple method.
Now, I can create the employee class in the same way.
All right, so we've created our classes, so now it's time to create a new object from each of these
classes.
So first I create an object from the person class and.
Look here, when I put a dot behind the name, I can use the save method.
Now, let's do the same to the customer class.
And now, look, I can use two methods, both in the person class and in the customer class.
Whoa, how can you do that?
Well, we've stated that we can use its properties and methods by extending this class person.
Class features were transferred to the customer class thanks to that the inheritance.
OK, so now when we do the same operations in the third class.
We can use these two methods again, both the methods specific to the class and the method that we will
use in common.
So as you can see, when we create a structure with common features, we do not have to write the same
features on the time we collect common features in one classroom.
Special features in other classes as our work becomes a lot easier when there's any change in the code
editing.
It's now before we finish up here, I do want to briefly introduce you to the idea of private and public
access and how do we actually define it by a whole lot better if we continue on the first car that we
wrote in a class.
All right.
So now, my friends, all the variables we've defined here are public just by default.
And that's just so that we don't have to write public every time.
So what a variable is public?
Well, just as it implies, we're allowed to access that variable from pretty much anywhere.
But if I come in here and write private and yeah, we'll make this very private now, see how we can't
show this very well outside of this class.
However, we can still use it inside the glass.
So here we can only use a variable in that class when we make it private.
All right.
Let's have a look and see what happens when we use a private variable in the person class.
So I'm using a private variable here.
Well, I can use this variable in the customer class.
No, I can't, because, as we said, this variable can only be used within the class in which it is
defined.
So if we define this variable as protected here.
We can now do what we want.
So protected access is pretty much the same logic as private, however, as you can see, it can be
used in inheritance classes.
We won't be able to use this variable outside as a public variable, you follow.
All right, well, let me ask you this, how can we use a private variable outside the class?
Well, I'm glad you bring that up.
Because we've got to talk about getter and setter methods.
All right, so let's have a look at the usage now.
So I'm defining a private variable here and I define the get method that will return this variable.
Then I write the set method that takes this variable.
And now I give a value to this variable outside of this class.
Now we can run these codes and see and here we can see the value that we return.
So by creating the getting set methods of a private variable, we were able to use these methods outside
of the class.
What do you think?
So sure enough, my friends, indeed this classroom is enough.
So in our next lesson, we're going to talk about interface.
I'll see you then.
Bye for now.