1 00:00:00,270 --> 00:00:03,330 In this lecture, we're going to learn about classes. 2 00:00:03,600 --> 00:00:10,290 You should already have an idea of how classes work in JavaScript classes and TypeScript work the same 3 00:00:10,290 --> 00:00:10,710 way. 4 00:00:11,010 --> 00:00:14,610 However, there are some additional features worth mentioning. 5 00:00:14,940 --> 00:00:16,890 Let's go through them one by one. 6 00:00:17,550 --> 00:00:21,340 First, let's create a class below the objects. 7 00:00:21,360 --> 00:00:24,540 We will define a class called investment accounts. 8 00:00:27,170 --> 00:00:33,050 At the end of the day, classes are blueprints for objects, we can add interfaces to them. 9 00:00:33,440 --> 00:00:35,480 The syntax is slightly different. 10 00:00:35,750 --> 00:00:39,950 After the class name, we need to type the implements keyword. 11 00:00:40,340 --> 00:00:45,350 This keyword will tell TypeScript we're trying to add an interface to the class. 12 00:00:45,650 --> 00:00:52,280 We can follow this keyword with the name of the interface will set the interface to IE accounts. 13 00:00:54,880 --> 00:01:01,450 After adding the interface, our editor will throw an error at us, it's telling us we need to implement 14 00:01:01,450 --> 00:01:04,930 the properties and methods required by our interface. 15 00:01:05,290 --> 00:01:06,790 Let's try adding them in. 16 00:01:07,150 --> 00:01:11,440 There are two required properties which are name and balance. 17 00:01:15,870 --> 00:01:19,230 It's optional to initialize the properties with values. 18 00:01:19,410 --> 00:01:25,110 We can leave them empty as long as we're adding them to the class, TypeScript is happy. 19 00:01:25,560 --> 00:01:32,400 In addition, we can initialize these properties by adding them to the constructor function in our class. 20 00:01:32,490 --> 00:01:34,710 We will add the constructor function. 21 00:01:37,330 --> 00:01:43,000 Inside the arguments of the constructor function, we will add both properties to this function. 22 00:01:46,180 --> 00:01:47,440 So far, so good. 23 00:01:47,860 --> 00:01:51,970 The last step is to add the public keyword to the parameters. 24 00:01:54,460 --> 00:01:59,710 The public keyword will allow our properties to be accessible outside of the class. 25 00:02:00,070 --> 00:02:06,730 If we add the public keyword to the constructors parameters, it's the same as if they were initialized 26 00:02:06,730 --> 00:02:07,570 like above. 27 00:02:07,960 --> 00:02:10,150 We can remove this part of the code. 28 00:02:10,570 --> 00:02:15,370 This shorthand method of initializing variables is common in angular. 29 00:02:15,730 --> 00:02:18,040 We'll be using this feature frequently. 30 00:02:18,670 --> 00:02:22,750 Speaking of public properties, we can make properties private. 31 00:02:23,050 --> 00:02:26,530 For example, let's add a method called withdraw. 32 00:02:29,210 --> 00:02:33,500 Let's say we don't want the withdraw method to be called outside the class. 33 00:02:33,860 --> 00:02:37,640 We can add the private modifier before the method name. 34 00:02:40,350 --> 00:02:45,000 By adding this keyword, the method can't be called outside the object. 35 00:02:45,330 --> 00:02:48,840 It must be called buying another method from within the class. 36 00:02:49,260 --> 00:02:55,260 It's a great way of preventing yourself from accidentally calling the wrong method or preventing a library 37 00:02:55,260 --> 00:02:56,160 from calling it. 38 00:02:56,760 --> 00:03:01,170 If we don't make a property or method private, they're public by default. 39 00:03:01,500 --> 00:03:06,030 We don't have to add the public keyword to most properties and methods. 40 00:03:06,360 --> 00:03:09,570 The exception to this rule is the constructor function. 41 00:03:10,020 --> 00:03:16,200 If we were to omit this keyword from the parameters, they will not be initialized as properties to 42 00:03:16,200 --> 00:03:23,520 the class classes or heavily used and angular will get a lot of practice with them, as well as learn 43 00:03:23,520 --> 00:03:25,140 some features along the way. 44 00:03:25,470 --> 00:03:30,480 In the next lecture, we'll move on from classes to learn about generics.