1 00:00:00,690 --> 00:00:02,177 ‫Instructor: Hi, within this lecture 2 00:00:02,177 --> 00:00:05,670 ‫we are going to learn about access levels. 3 00:00:05,670 --> 00:00:07,560 ‫So what are those? 4 00:00:07,560 --> 00:00:10,920 ‫They define where we can reach the variable 5 00:00:10,920 --> 00:00:14,670 ‫or functions that we have created. Okay? 6 00:00:14,670 --> 00:00:16,350 ‫We have five of them. 7 00:00:16,350 --> 00:00:21,350 ‫It starts with private and it goes down to the open. 8 00:00:21,990 --> 00:00:25,260 ‫So, let me start with open and public. 9 00:00:25,260 --> 00:00:27,660 ‫We don't generally use them. 10 00:00:27,660 --> 00:00:28,493 ‫Why? 11 00:00:28,493 --> 00:00:31,530 ‫Because they're not kind of safe to implement. 12 00:00:31,530 --> 00:00:34,920 ‫If you make something open, it means that 13 00:00:34,920 --> 00:00:38,910 ‫it can be reached from anywhere in the project 14 00:00:38,910 --> 00:00:43,290 ‫and it can be changed from anywhere in the project. 15 00:00:43,290 --> 00:00:47,190 ‫Public also can be reached from anywhere 16 00:00:47,190 --> 00:00:50,250 ‫but they cannot be changed. 17 00:00:50,250 --> 00:00:54,900 ‫So, why are we not using them and who uses them anyway? 18 00:00:54,900 --> 00:00:57,660 ‫Within the next sections we are going to work with 19 00:00:57,660 --> 00:00:59,460 ‫external libraries. 20 00:00:59,460 --> 00:01:03,600 ‫So we are going to just download some software kit, 21 00:01:03,600 --> 00:01:07,380 ‫development kits as the case to our project. 22 00:01:07,380 --> 00:01:10,920 ‫For example, in order to work with a cloud server 23 00:01:10,920 --> 00:01:13,620 ‫in the case of Firebase we are going to see 24 00:01:13,620 --> 00:01:16,860 ‫how we are going to do that, don't worry about it. 25 00:01:16,860 --> 00:01:19,920 ‫But right now know that we are going to implement 26 00:01:19,920 --> 00:01:24,360 ‫and download some project files into our own project. 27 00:01:24,360 --> 00:01:28,920 ‫And these Firebased developers make their files public 28 00:01:28,920 --> 00:01:33,920 ‫so that we can reach them and use them in our own project. 29 00:01:34,380 --> 00:01:37,890 ‫But other than that, there is no way, 30 00:01:37,890 --> 00:01:42,890 ‫there is not actually a logic to implement public and open. 31 00:01:42,930 --> 00:01:46,410 ‫Rather, we are going to work private file private 32 00:01:46,410 --> 00:01:47,913 ‫and internal mostly. 33 00:01:48,930 --> 00:01:51,960 ‫So private means this is actually private 34 00:01:51,960 --> 00:01:56,490 ‫this is special to the class that we have created in. 35 00:01:56,490 --> 00:02:00,600 ‫It cannot be reached from anywhere else. 36 00:02:00,600 --> 00:02:04,290 ‫So if you want to make it private then you have to 37 00:02:04,290 --> 00:02:07,860 ‫be careful because you cannot reach it from somewhere else. 38 00:02:07,860 --> 00:02:10,620 ‫And internal is the default one. 39 00:02:10,620 --> 00:02:15,120 ‫And file private means you get to reach it from the file. 40 00:02:15,120 --> 00:02:18,690 ‫For example, you get to reach it from this musician's 41 00:02:18,690 --> 00:02:20,250 ‫dot swift file. 42 00:02:20,250 --> 00:02:25,250 ‫Not in the class only, but from this file. Okay? 43 00:02:25,560 --> 00:02:28,440 ‫So let me make an example because you will 44 00:02:28,440 --> 00:02:31,170 ‫understand it much better this way. 45 00:02:31,170 --> 00:02:36,060 ‫For example, let me make this function sync private. 46 00:02:36,060 --> 00:02:39,930 ‫So if I make this private, it means that I can only 47 00:02:39,930 --> 00:02:44,670 ‫reach this sync function within my musicians class. 48 00:02:44,670 --> 00:02:48,633 ‫I cannot reach it from anywhere else in my project. 49 00:02:49,950 --> 00:02:54,950 ‫So, let's try, actually I made this private, right? 50 00:02:55,170 --> 00:02:59,430 ‫So, can I call this from main dot swift 51 00:02:59,430 --> 00:03:02,940 ‫or can I call this function from other 52 00:03:02,940 --> 00:03:05,550 ‫classes or other swift files? 53 00:03:05,550 --> 00:03:06,720 ‫I cannot do that. 54 00:03:06,720 --> 00:03:10,020 ‫If I do command B or if I try to run this, 55 00:03:10,020 --> 00:03:13,800 ‫as you can see, it gives me an error and it says that sync 56 00:03:13,800 --> 00:03:17,790 ‫is inaccessible due to private protection levels. 57 00:03:17,790 --> 00:03:21,750 ‫So James cannot sing right now because I made it private. 58 00:03:21,750 --> 00:03:25,110 ‫And in the super musician class also, 59 00:03:25,110 --> 00:03:27,480 ‫even though I'm inheriting this 60 00:03:27,480 --> 00:03:30,990 ‫I cannot reach the function sync. 61 00:03:30,990 --> 00:03:35,760 ‫So if I make this private, they cannot call this 62 00:03:35,760 --> 00:03:39,540 ‫sync function from anywhere in my project. 63 00:03:39,540 --> 00:03:42,930 ‫So it doesn't make sense to make this private 64 00:03:42,930 --> 00:03:45,810 ‫because I'm inheriting this function. 65 00:03:45,810 --> 00:03:49,260 ‫I'm using this function in my objects. 66 00:03:49,260 --> 00:03:52,860 ‫So, I believe we should delete this. 67 00:03:52,860 --> 00:03:56,910 ‫But there will be some cases that you may want to 68 00:03:56,910 --> 00:03:58,950 ‫make things private. 69 00:03:58,950 --> 00:04:03,090 ‫For example, if you try to come up with a variable inside of 70 00:04:03,090 --> 00:04:06,270 ‫your main dot swift or inside of your weave controller 71 00:04:06,270 --> 00:04:09,150 ‫and if you're not going to use that variable inside 72 00:04:09,150 --> 00:04:13,980 ‫of this another class, for example, you can make it private 73 00:04:13,980 --> 00:04:16,590 ‫and you can make it for functions as well. 74 00:04:16,590 --> 00:04:19,290 ‫For example, in my musician dot swift 75 00:04:19,290 --> 00:04:22,950 ‫let's suppose that I have a test function, okay? 76 00:04:22,950 --> 00:04:27,950 ‫And I'm just testing something so I can print out test 77 00:04:28,170 --> 00:04:29,640 ‫for example, 78 00:04:29,640 --> 00:04:34,500 ‫let's say that we are again working in a big group 79 00:04:34,500 --> 00:04:38,550 ‫so I'm developing musicians class and I'm testing something 80 00:04:38,550 --> 00:04:43,410 ‫but I don't want anyone to create a musician object 81 00:04:43,410 --> 00:04:45,750 ‫and reach the test function. 82 00:04:45,750 --> 00:04:50,520 ‫So as you can see they cannot call test from main dot swift. 83 00:04:50,520 --> 00:04:54,450 ‫So let me do a command B, as you can see, they can call sing 84 00:04:54,450 --> 00:04:56,943 ‫but they cannot call test. 85 00:04:57,990 --> 00:05:01,830 ‫So this is good, right? We can make things private 86 00:05:01,830 --> 00:05:05,160 ‫if we want to make them really private 87 00:05:05,160 --> 00:05:08,970 ‫if we want to use them only within the current class. 88 00:05:08,970 --> 00:05:12,150 ‫And we can make it file private if we want to 89 00:05:12,150 --> 00:05:16,920 ‫use it inside of that particular file. 90 00:05:16,920 --> 00:05:21,090 ‫So, this is all we want to talk about access levels 91 00:05:21,090 --> 00:05:23,400 ‫and this is all we want to talk about objective 92 00:05:23,400 --> 00:05:24,990 ‫oriented programming. 93 00:05:24,990 --> 00:05:27,630 ‫So we are gonna stop here and within the next lecture 94 00:05:27,630 --> 00:05:30,480 ‫we're going to create a model and display 95 00:05:30,480 --> 00:05:33,393 ‫all of this information in a real app.