1 00:00:00,120 --> 00:00:04,770 Now, let's look at something that is called visibility modifier or access modifier. 2 00:00:05,800 --> 00:00:11,680 So in Scotland, they're called visibility modifiers, and they are the key word which is used to restrict 3 00:00:11,680 --> 00:00:14,920 the use of classes, interface methods and properties in Scotland. 4 00:00:15,640 --> 00:00:17,440 So what is a visibility modifier? 5 00:00:17,650 --> 00:00:23,440 Well, was ability modifiers are the key words which are used to restrict the use of classes, interfaces, 6 00:00:23,440 --> 00:00:25,120 methods and properties in Scotland. 7 00:00:25,450 --> 00:00:30,580 And these modifiers are used at multiple places such as the class header or the method. 8 00:00:30,580 --> 00:00:37,240 Body and visibility modifiers are categorised into four different types which are public, private, 9 00:00:37,240 --> 00:00:39,040 protected and internal. 10 00:00:39,280 --> 00:00:44,950 The most common ones are public and private, and you probably have used them already, or at least 11 00:00:44,950 --> 00:00:46,150 have seen them already. 12 00:00:46,480 --> 00:00:48,160 So let's start with a public modifier. 13 00:00:48,190 --> 00:00:51,700 A public modified element is accessible from everywhere in the project. 14 00:00:52,330 --> 00:00:58,270 The public modifier is the default modifier in Scotland, so if any class interface etc is not specified 15 00:00:58,270 --> 00:01:05,710 with any access or visibility modifier, then that class or interface or and so forth is used in a public 16 00:01:05,710 --> 00:01:06,160 scope. 17 00:01:06,310 --> 00:01:09,760 So if you had nothing, then that element is automatically public. 18 00:01:10,900 --> 00:01:14,320 All public declarations can be placed at the top of the file. 19 00:01:14,770 --> 00:01:18,700 If a member of a class is not specified, then it is by default public. 20 00:01:18,910 --> 00:01:23,560 So here the same goes as for interfaces and classes and so forth. 21 00:01:24,340 --> 00:01:27,160 So let's have a look at the syntax of public modifiers. 22 00:01:27,340 --> 00:01:33,160 As you can see, you can make a class public by adding the public key word or it is automatically public 23 00:01:33,160 --> 00:01:36,370 if you don't edit, but that's generally what you can do. 24 00:01:36,400 --> 00:01:45,430 You can add the public keyword specifically state that a class or a method or a value even form a field 25 00:01:45,790 --> 00:01:48,190 variable, however, is public. 26 00:01:49,920 --> 00:01:53,490 Then the private modifier, and that's where things are could become a little more interesting. 27 00:01:53,880 --> 00:01:59,100 Private modifier allows the elements to be accessible only within the block in which properties, fields, 28 00:01:59,100 --> 00:02:00,630 etc. are declared. 29 00:02:00,930 --> 00:02:06,870 So if something is private, then it's only accessible where it is declared. 30 00:02:07,410 --> 00:02:07,800 All right. 31 00:02:08,460 --> 00:02:14,160 So the private modifier declaration does not allow access outside of the scope, and the private package 32 00:02:14,160 --> 00:02:17,220 can be accessible within that specific file. 33 00:02:17,220 --> 00:02:22,650 So if you have multiple packages and you make one package, for example private, then this one will 34 00:02:22,650 --> 00:02:25,110 not be accessible in other files. 35 00:02:25,230 --> 00:02:30,180 So, for example, if you have multiple classes and you have a package in one class and you make this 36 00:02:30,180 --> 00:02:35,580 package private, then you cannot use it in other classes which are in other files. 37 00:02:35,790 --> 00:02:36,150 All right. 38 00:02:37,140 --> 00:02:39,020 So the syntax for a private modifier? 39 00:02:39,090 --> 00:02:45,390 Well, you can have a class that is private, then you can have its properties to be private by just 40 00:02:45,390 --> 00:02:49,590 adding this private keyword, then to methods to be private. 41 00:02:49,920 --> 00:02:55,710 And and this above example, you can see that Volks and the function do something are declared as private. 42 00:02:56,070 --> 00:03:01,740 The class example is accessible from the same source file vault and fondue. 43 00:03:01,740 --> 00:03:04,530 Something are only accessible within the sample class. 44 00:03:04,800 --> 00:03:12,570 So if we now create an object of this class example, we cannot access the variable X and we cannot 45 00:03:12,570 --> 00:03:14,520 access the method to do something. 46 00:03:14,820 --> 00:03:15,240 OK? 47 00:03:15,480 --> 00:03:21,120 So they're are really only available within that same class at this class is only available within that 48 00:03:21,120 --> 00:03:21,780 same file. 49 00:03:22,260 --> 00:03:27,950 So if we use another file, this class will not be accessible and we cannot create objects of it, then 50 00:03:27,960 --> 00:03:29,550 then turn on modifier as a feature. 51 00:03:29,550 --> 00:03:35,280 And cotton, which is not available in Java and the internal modifier makes the field visible only inside 52 00:03:35,280 --> 00:03:37,650 the module in which it is implemented. 53 00:03:38,220 --> 00:03:44,220 So all fields are declared as internal, which are accessible only inside the module in which they are 54 00:03:44,220 --> 00:03:45,060 implemented. 55 00:03:46,090 --> 00:03:46,450 All right. 56 00:03:46,870 --> 00:03:52,660 So another example here, and we can see the syntax for internal modifiers, so you can use this internal 57 00:03:52,660 --> 00:03:56,170 keyword for a class, for a property. 58 00:03:56,530 --> 00:04:00,880 And also for methods and even for global variables, as you can see here. 59 00:04:03,360 --> 00:04:09,330 So then there is a keyword called open, and we need to talk about it real quick in order to understand 60 00:04:09,330 --> 00:04:10,120 the next concept. 61 00:04:10,160 --> 00:04:16,110 So in Scotland, all classes are final by default, so they can't be inherited by default. 62 00:04:16,380 --> 00:04:16,640 OK. 63 00:04:16,680 --> 00:04:19,140 It's different to other programming languages. 64 00:04:19,140 --> 00:04:24,960 For example, in Java, it's the opposite that you have to make your class final explicitly because 65 00:04:24,960 --> 00:04:26,730 all classes can be inherited from. 66 00:04:27,210 --> 00:04:34,440 So if you want to use inheritance, then you need to make the class that you want to inherit from open. 67 00:04:35,070 --> 00:04:40,590 So to make a class inheritable to other classes, you must market with the open keyword and otherwise 68 00:04:40,590 --> 00:04:41,370 you get an error here. 69 00:04:41,370 --> 00:04:43,260 Type is final, so can't be inherited. 70 00:04:43,920 --> 00:04:44,280 All right. 71 00:04:44,280 --> 00:04:51,690 So then there is the protected modifier with a class or an interface allows visibility to its class 72 00:04:51,690 --> 00:04:52,830 or subclass. 73 00:04:53,310 --> 00:04:59,610 A protected declaration when overridden in its subclass is also protected unless it is explicitly changed. 74 00:04:59,620 --> 00:05:05,050 So we're going to look at that in a bit, and the protected modifier cannot be declared at the top level. 75 00:05:05,070 --> 00:05:07,470 So packages cannot be protected. 76 00:05:08,250 --> 00:05:13,470 You can use other visibility modifiers, but you cannot use the protected modifier on packages. 77 00:05:14,400 --> 00:05:20,490 So here's an example you can see here we're using the open keyword open class base where we use this 78 00:05:20,490 --> 00:05:23,490 protected veil I, which we set to zero. 79 00:05:24,120 --> 00:05:29,550 So now we have this clause, which derives from base, so which is our derived class. 80 00:05:30,030 --> 00:05:32,130 And there we have this function. 81 00:05:32,130 --> 00:05:36,510 Get value for this method to be precise, and it returns an integer. 82 00:05:36,870 --> 00:05:39,780 And there we now return I. 83 00:05:40,020 --> 00:05:48,990 So we'll return the variable that was created in the base class because we are inheriting from it. 84 00:05:49,260 --> 00:05:53,030 We can use it, but if we weren't inheriting from it. 85 00:05:53,100 --> 00:05:59,010 So if we're in another class that's called this Class C, then we cannot use this vow. 86 00:05:59,280 --> 00:05:59,640 I. 87 00:06:01,710 --> 00:06:04,910 Then another example of a overriding of protected types. 88 00:06:04,980 --> 00:06:09,130 So here we have an open protected value. 89 00:06:09,480 --> 00:06:13,080 So now it allows us to override that value. 90 00:06:13,320 --> 00:06:18,420 OK, so now we can override it, we cannot just only use it but actually override it. 91 00:06:18,870 --> 00:06:24,510 So you can see we have another class which also inherits from base so different to the one that we had 92 00:06:24,510 --> 00:06:24,930 before. 93 00:06:25,260 --> 00:06:28,800 As you can see, this was class derived, and now we have this class called another. 94 00:06:29,220 --> 00:06:30,750 It derives from bass. 95 00:06:31,020 --> 00:06:35,700 It also has a function called get value and then it returns II. 96 00:06:36,420 --> 00:06:39,390 But on top of that, it overrides the Value II. 97 00:06:39,420 --> 00:06:44,730 So here you can see if you want to be able to write something you need to add to open keywords to it. 98 00:06:45,150 --> 00:06:50,010 So in this case, you need to open the value that we use there, the variable. 99 00:06:50,610 --> 00:06:54,140 All right now, we have another example here of the visibility modifier. 100 00:06:54,150 --> 00:06:58,770 So we have this open class space where you make it open so that we can inherit from it. 101 00:06:59,110 --> 00:07:03,570 Our other classes, like our derived class at the bottom, can inherit from it. 102 00:07:03,990 --> 00:07:09,920 So we're a that we define as automatically public because it's public by default, right? 103 00:07:09,930 --> 00:07:13,530 We didn't specifically state that it's public, but it is by default public. 104 00:07:14,040 --> 00:07:17,550 Then we create this other variable called beam and that one is private. 105 00:07:17,880 --> 00:07:20,070 So it's private to the base class. 106 00:07:20,670 --> 00:07:23,040 Then we have this protected verbal. 107 00:07:24,110 --> 00:07:26,060 See, and we make it open. 108 00:07:26,510 --> 00:07:34,130 So this one is visible to the base and it derived class, which means that the derived class in this 109 00:07:34,130 --> 00:07:38,840 case, it's called drive, but this is the case for all classes that are deriving. 110 00:07:40,050 --> 00:07:41,880 It's visible there and it's usable there. 111 00:07:42,630 --> 00:07:49,530 Then we have this internal Valda, which is visible inside the same module, which means that it is 112 00:07:49,530 --> 00:07:53,790 visible inside of the same file, pretty much. 113 00:07:54,630 --> 00:08:01,020 And then we have this protected fund, which is our function, and it's visible to the base and the 114 00:08:01,020 --> 00:08:01,830 draft class. 115 00:08:02,250 --> 00:08:09,180 So because it's protected, it's not only visible or usable in our base class, but also in our derived 116 00:08:09,180 --> 00:08:12,900 class, but not inside of our fund. 117 00:08:12,900 --> 00:08:13,230 Main. 118 00:08:13,470 --> 00:08:13,900 All right. 119 00:08:13,920 --> 00:08:15,660 And there it's not a visible. 120 00:08:17,730 --> 00:08:23,190 So here we have already arrived, plus a C, D and E of the base class are visible. 121 00:08:23,580 --> 00:08:24,540 B is not visible. 122 00:08:24,780 --> 00:08:25,350 Why is that? 123 00:08:25,380 --> 00:08:29,670 Well, because it's private, so it's only available in the base class itself. 124 00:08:30,420 --> 00:08:36,510 Protected internal and public variables, however, are visible in the draft class and then we can even 125 00:08:36,510 --> 00:08:44,340 override the value C because it's protected so it can be used in the draft class, but it's also open 126 00:08:44,340 --> 00:08:45,630 so we can override it. 127 00:08:46,080 --> 00:08:49,170 OK, this is protected, but we can override with the override. 128 00:08:49,170 --> 00:08:49,620 Key word. 129 00:08:50,850 --> 00:08:59,100 Then we have our function mate, and in there we have a base object, so we create an object of our 130 00:08:59,100 --> 00:09:02,940 base class and we create an object of our derived class. 131 00:09:03,480 --> 00:09:03,870 All right. 132 00:09:04,350 --> 00:09:10,260 So when we try to access A and D, we can access them because A. 133 00:09:10,290 --> 00:09:12,560 S public and D. 134 00:09:13,880 --> 00:09:18,800 His internal so it's visible in the same module, and we are in the same module in this example. 135 00:09:19,460 --> 00:09:26,750 And then we have base B, C and E, which are all not visible because they are protected, so they are 136 00:09:26,750 --> 00:09:30,530 only visible in the same class or in a driving class. 137 00:09:31,400 --> 00:09:31,820 All right. 138 00:09:31,820 --> 00:09:32,180 So. 139 00:09:33,270 --> 00:09:38,640 PE is a private one, so it's not going to be visible anywhere else, right, except, of course, for 140 00:09:38,640 --> 00:09:39,270 the base class. 141 00:09:39,660 --> 00:09:44,010 Then we have to value see which is protected. 142 00:09:44,130 --> 00:09:49,350 So it's not visible outside of classes that are driving and the class itself. 143 00:09:49,920 --> 00:09:52,530 And then we have the same forty four hour function. 144 00:09:53,670 --> 00:09:57,960 All right, and then we have the vault derived object. 145 00:09:58,590 --> 00:10:03,690 And here we are trying to access C, but C is not visible. 146 00:10:04,080 --> 00:10:04,680 Why is that? 147 00:10:05,070 --> 00:10:08,220 Well, because we are inheriting. 148 00:10:10,050 --> 00:10:16,200 Our values see, we're overriding the value see, but it's still going to be protected. 149 00:10:16,740 --> 00:10:22,020 So C is not visible, so this is a little bit much to digest, I can imagine. 150 00:10:22,410 --> 00:10:25,410 So no worries, but we're going to use this quite a bit. 151 00:10:25,410 --> 00:10:32,130 So we're going to use all of these visibility modifiers when we need them and most of them, we are 152 00:10:32,130 --> 00:10:34,230 going to use very sparingly, very rarely. 153 00:10:34,560 --> 00:10:40,020 So, for example, protected and internal are not used as often as, for example, the private key word. 154 00:10:40,410 --> 00:10:44,420 So we're going to use that one and step by step, they all will become natural. 155 00:10:44,430 --> 00:10:48,480 So no worries if that was a little bit much at all at once. 156 00:10:48,660 --> 00:10:53,610 But I wanted to keep it all together in one video so you can see the different rules there.