In this lecture, we're going to learn about classes.
You should already have an idea of how classes work in JavaScript classes and TypeScript work the same
way.
However, there are some additional features worth mentioning.
Let's go through them one by one.
First, let's create a class below the objects.
We will define a class called investment accounts.
At the end of the day, classes are blueprints for objects, we can add interfaces to them.
The syntax is slightly different.
After the class name, we need to type the implements keyword.
This keyword will tell TypeScript we're trying to add an interface to the class.
We can follow this keyword with the name of the interface will set the interface to IE accounts.
After adding the interface, our editor will throw an error at us, it's telling us we need to implement
the properties and methods required by our interface.
Let's try adding them in.
There are two required properties which are name and balance.
It's optional to initialize the properties with values.
We can leave them empty as long as we're adding them to the class, TypeScript is happy.
In addition, we can initialize these properties by adding them to the constructor function in our class.
We will add the constructor function.
Inside the arguments of the constructor function, we will add both properties to this function.
So far, so good.
The last step is to add the public keyword to the parameters.
The public keyword will allow our properties to be accessible outside of the class.
If we add the public keyword to the constructors parameters, it's the same as if they were initialized
like above.
We can remove this part of the code.
This shorthand method of initializing variables is common in angular.
We'll be using this feature frequently.
Speaking of public properties, we can make properties private.
For example, let's add a method called withdraw.
Let's say we don't want the withdraw method to be called outside the class.
We can add the private modifier before the method name.
By adding this keyword, the method can't be called outside the object.
It must be called buying another method from within the class.
It's a great way of preventing yourself from accidentally calling the wrong method or preventing a library
from calling it.
If we don't make a property or method private, they're public by default.
We don't have to add the public keyword to most properties and methods.
The exception to this rule is the constructor function.
If we were to omit this keyword from the parameters, they will not be initialized as properties to
the class classes or heavily used and angular will get a lot of practice with them, as well as learn
some features along the way.
In the next lecture, we'll move on from classes to learn about generics.