1 00:00:05,000 --> 00:00:11,000 Hello, dear students, in this lesson, we'll talk about encapsulation and we'll learn what is the 2 00:00:11,000 --> 00:00:15,000 role of encapsulation principle in the object oriented programming. 3 00:00:15,000 --> 00:00:17,000 In this lesson, we'll talk with you. 4 00:00:17,000 --> 00:00:22,000 Why do we need encapsulation and how we can implement encapsulation in our programs? 5 00:00:22,000 --> 00:00:28,000 We'll learn with your access modifiers and on real court examples, I will show you how you can use 6 00:00:28,000 --> 00:00:28,000 them. 7 00:00:28,000 --> 00:00:34,000 I will share with you my practical experience when to use which modifier and will learn the rules which 8 00:00:34,000 --> 00:00:36,000 we need to follow during overwriting. 9 00:00:36,000 --> 00:00:40,000 So let's jump to examples to understand this topic better. 10 00:00:41,000 --> 00:00:42,000 What is an observation? 11 00:00:43,000 --> 00:00:48,000 Encapsulation is a principle that teaches us to group data and behavior those our fields and methods 12 00:00:49,000 --> 00:00:49,000 together. 13 00:00:49,000 --> 00:00:56,000 As a single unit, we can control access to interfaces, classes, fields, methods to control access. 14 00:00:56,000 --> 00:00:58,000 We have four main access modifiers. 15 00:00:59,000 --> 00:01:04,000 They are public protected package, private or default private. 16 00:01:05,000 --> 00:01:10,000 Let's start one by one to understand the difference on the specific examples, to not show you how access 17 00:01:10,000 --> 00:01:13,000 modifiers work with classes, fields and methods. 18 00:01:13,000 --> 00:01:19,000 Separately, I will show you how Access Modifier works with methods, because these modifiers control 19 00:01:19,000 --> 00:01:25,000 access to elements and the way how they work are similar, no matter whether you're trying to get access 20 00:01:25,000 --> 00:01:26,000 for field or method. 21 00:01:27,000 --> 00:01:34,000 OK, so imagine we have some class, let's call it Class A. And inside this class we have the method 22 00:01:34,000 --> 00:01:35,000 with public access modifier. 23 00:01:36,000 --> 00:01:39,000 That means you can access it from any point of your program. 24 00:01:39,000 --> 00:01:44,000 For example, here you can see them a class which is outside of our package. 25 00:01:44,000 --> 00:01:44,000 No. 26 00:01:44,000 --> 00:01:49,000 One, I can create the instance of Class A and call the public method on it. 27 00:01:50,000 --> 00:01:53,000 Now imagine I have protected MassArt. 28 00:01:53,000 --> 00:01:57,000 Do something to will I be able to call it from them a class? 29 00:01:57,000 --> 00:02:04,000 Unfortunately, no way, because I can get access to protected entities only inside the same package 30 00:02:04,000 --> 00:02:06,000 or in a child class. 31 00:02:06,000 --> 00:02:12,000 If only I had them a class inside the package, no one I would be able to call method to do something 32 00:02:12,000 --> 00:02:14,000 to see here. 33 00:02:15,000 --> 00:02:19,000 I can call this method when demo class is in the same package with class A. 34 00:02:20,000 --> 00:02:28,000 Also here I have a class of the class A, package number two and inside the do something class B I can 35 00:02:28,000 --> 00:02:29,000 call protective measures. 36 00:02:29,000 --> 00:02:30,000 Do something too. 37 00:02:30,000 --> 00:02:37,000 So now you understand that I can access protected entities in case they're in the same package or in 38 00:02:37,000 --> 00:02:38,000 child classes. 39 00:02:39,000 --> 00:02:41,000 Our next modifier is package private. 40 00:02:42,000 --> 00:02:43,000 You can see that do something. 41 00:02:43,000 --> 00:02:47,000 Class B doesn't have any access modifier specified. 42 00:02:47,000 --> 00:02:48,000 That is not true. 43 00:02:49,000 --> 00:02:54,000 When we don't specify access modifiers, that means that access to this entity is a default. 44 00:02:55,000 --> 00:02:58,000 So usually absence of any keywords are associated with access. 45 00:02:58,000 --> 00:03:02,000 Modifier is called package primary or just default access. 46 00:03:02,000 --> 00:03:04,000 So let me get back to them. 47 00:03:04,000 --> 00:03:10,000 A class and package number one in scope of this package, I will create the object of the Class B type. 48 00:03:10,000 --> 00:03:17,000 And on this object I call do something Class B method and you see that I have a compilation error. 49 00:03:17,000 --> 00:03:24,000 But if I create object of Class B type inside the package number two and call the same method, it is 50 00:03:24,000 --> 00:03:28,000 possible because default access is only within the same package. 51 00:03:29,000 --> 00:03:33,000 Take into account this demo class and Class B in the same package. 52 00:03:33,000 --> 00:03:39,000 We don't have compilation error and last but not least, modifier is private modifier. 53 00:03:39,000 --> 00:03:41,000 Everything is easy here. 54 00:03:41,000 --> 00:03:44,000 You can access private entities only in scope. 55 00:03:44,000 --> 00:03:49,000 When it was declared, for example, I have private method declared in this class and I can call this 56 00:03:49,000 --> 00:03:56,000 private method wherever I want in this class, but I can't call this method outside of the class, even 57 00:03:56,000 --> 00:03:58,000 in the same package. 58 00:03:58,000 --> 00:04:01,000 If I will try to do so, I see compilation error. 59 00:04:02,000 --> 00:04:05,000 So now you understand the difference between modifiers. 60 00:04:05,000 --> 00:04:11,000 I believe you understood that encapsulation in all AP language can be implemented with the help of access 61 00:04:11,000 --> 00:04:14,000 modifiers and you know how they work. 62 00:04:15,000 --> 00:04:19,000 Knowing this during the planning of architecture, you can choose access modifier, which works the 63 00:04:19,000 --> 00:04:20,000 best for you. 64 00:04:21,000 --> 00:04:27,000 I want to share with you, which modifies you will use most of the times in 99 percent of cases you 65 00:04:27,000 --> 00:04:29,000 will use is a private or public. 66 00:04:29,000 --> 00:04:35,000 This is because you may have very few business cases when you want to keep something visible only in 67 00:04:35,000 --> 00:04:39,000 scope of the package or only in scope of the package and child class. 68 00:04:40,000 --> 00:04:44,000 Most likely you will implement interfaces and would like to keep the behavior public. 69 00:04:45,000 --> 00:04:50,000 Sometimes you will have methods which are needed only for this class and you will keep them private. 70 00:04:50,000 --> 00:04:56,000 Usually all fields have the private access modifier because you want to control interaction with your 71 00:04:56,000 --> 00:04:57,000 object. 72 00:04:57,000 --> 00:05:03,000 So now you understand that default and protected modifiers are not used very often. 73 00:05:04,000 --> 00:05:09,000 And last thing for the day, which I wanted to discuss with you, is the rules for access modifiers 74 00:05:09,000 --> 00:05:09,000 during that method. 75 00:05:09,000 --> 00:05:12,000 Overriding the rule is simple. 76 00:05:12,000 --> 00:05:17,000 When you override the method, you can't reduce the visibility of the inherited method. 77 00:05:17,000 --> 00:05:21,000 For example, in class a way of do something method. 78 00:05:21,000 --> 00:05:27,000 Class B extends class A and class C extends class B, is that clear? 79 00:05:27,000 --> 00:05:35,000 And now I want to do something method and I override it was protected to modify it just to recall it 80 00:05:35,000 --> 00:05:37,000 was public and parent class. 81 00:05:37,000 --> 00:05:43,000 And I have a compilation error here because we can't narrow access. 82 00:05:43,000 --> 00:05:46,000 On the other hand, in Class B, do something. 83 00:05:46,000 --> 00:05:49,000 Class B method has default access modifier. 84 00:05:49,000 --> 00:05:50,000 I can increase access level. 85 00:05:53,000 --> 00:05:59,000 And you can see here that I overwrite my head with public modifier and there is no compilation error 86 00:05:59,000 --> 00:06:03,000 because you can extend access now, that's it. 87 00:06:04,000 --> 00:06:06,000 Now, let's recall what we have learned today. 88 00:06:07,000 --> 00:06:09,000 Today, we learned what an encapsulation is. 89 00:06:09,000 --> 00:06:13,000 We know that access modifiers can help us to implement encapsulation. 90 00:06:13,000 --> 00:06:20,000 Principle in our code also will learn the difference between public protected default and private access 91 00:06:20,000 --> 00:06:21,000 modifiers. 92 00:06:21,000 --> 00:06:27,000 We discussed which modifiers developers use more often, and we saw specific rules which we need to 93 00:06:27,000 --> 00:06:29,000 follow during override. 94 00:06:29,000 --> 00:06:32,000 And that's all what I had in my agenda for today. 95 00:06:32,000 --> 00:06:34,000 Thanks a lot for your attention. 96 00:06:34,000 --> 00:06:35,000 And see you in the next lesson.